#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
using namespace std;

set<string> st;

void BFS(int n, string s) {
    queue<string> que;
    que.push(s);          //压入初始状态
    st.insert(s);
    int cnt = 0;
    while (!que.empty()) {
        int size = que.size();
        for (int i = 0; i < size; i++) {
            string tmp = que.front();
            que.pop();
            if (tmp.find("2012") != string::npos) {
                cout << cnt << endl;
                return;
            }
            for (int j = 0; j < n - 1; j++) {
                string tmp1 = tmp;
                char c = tmp1[j];
                tmp1[j] = tmp1[j+1];
                tmp1[j+1] = c;
                if (st.find(tmp1) == st.end()) {
                    que.push(tmp1);
                }
            }
        }
        cnt++;
    }
    if (cnt == 0) cout << "-1" << endl;
}

int main() 
{
    int N;
    while (cin >> N) {
        string s;
        cin >> s;
        BFS(N, s);
    }
    return 0;
}