#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n;
string start;

bool check(string s) {
	int len = s.length();
	for (int i = 0; i < len - 3; i++) {
		if (s[i] == '2' && s[i + 1] == '0' && s[i + 2] == '1' && s[i + 3] == '2') {
			return true;
		}
	}
	return false;
}

int bfs(string start) {
	queue<string> q;
	unordered_map<string, int> d;
	q.push(start);
	d[start] = 0;

	while (q.size()) {
		auto t = q.front();
		q.pop();
		int dist = d[t];
		if (check(t)) return dist;
		for (int i = 0; i < n - 1; i++) {
			swap(t[i], t[i + 1]);
			if (!d.count(t)) {
				d[t] = dist + 1;
				q.push(t);
			}
			swap(t[i], t[i + 1]);
		}
	}
	return -1;
}

int main() {
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			char c;
			cin >> c;
			start += c;
		}
		cout << bfs(start) << endl;
	}
	return 0;
}