//土尔逊Torson 编写于2023/06/14
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <queue>
#include <string>

using namespace std;

struct num12101 {
	int index;
	string s;
	num12101(int i, string ss):index(i),s(ss){}
};

void bfs12101(string s) {
	queue<num12101> q;
	q.push(num12101(0, s));
	while (!q.empty()) {
		num12101 g = q.front();
		q.pop();
		string gs = g.s;
		if (gs.find("2012") != string::npos) {
			printf("%d\n", g.index);
			return;
		}
		for (int i = 0; i < gs.size() - 1; i++) {
			swap(gs[i], gs[i + 1]);
			q.push(num12101(g.index + 1, gs));
			swap(gs[i], gs[i + 1]);
		}
	}
	printf("-1\n");
}

int main() {
	int n;
	string m;
	while (scanf("%d", &n) != EOF) {
		cin >> m;
		bfs12101(m);
	}
	system("pause");
	return EXIT_SUCCESS;
}
// 64 位输出请用 printf("%lld")