#include <bits/stdc++.h>
using namespace std;
unordered_map<string, int> d;
unordered_map<string, bool> st;
string str;
int bfs(){
queue<string> q;
st[str] = true;
q.push(str);
while (!q.empty()){
string t = q.front();
q.pop();
if (t.find("2012") != t.npos){
return d[t];
}
for (int i=1; i<t.size(); i++){
string tt = t;
swap(tt[i], tt[i-1]);
if (!st[tt]){
st[tt] = true;
q.push(tt);
d[tt] = d[t] + 1;
}
}
}
return -1;
}
int main(){
int n;
while (cin >> n){
cin >> str;
cout << bfs() << endl;
}
return 0;
}