#include <iostream>
#include <unordered_map>
#include <queue>
using namespace std;
int bfs(string s){
unordered_map<string, int> d;
queue<string> q;
q.push(s);
d[s]=0;
while(q.size()){
string t = q.front();
string temp = t;
q.pop();
if(t.find("2012")!=-1)return d[t];
for(int i=0;i<s.size()-1;i++){
swap(t[i],t[i+1]);
if(!d.count(t)){
q.push(t);
d[t]=d[temp]+1;
//cout<<d[t]<<endl;
}
t=temp;
}
}
return -1;
}
int main() {
int n;
while(cin>>n){
string s;
cin>>s;
cout<<bfs(s);
}
return 0;
}

京公网安备 11010502036488号