#include <iostream> #include<map> #include<string> #include<set> #include<queue> using namespace std; string aim; void bfs(string str) { queue<string>q; q.push(str); map<string, int>visit; bool isfind = false; while (!q.empty()) { string t = q.front(); visit[t]++; if (t.find("2012")!=string::npos) { isfind = true; cout << visit[t] - 1 << endl; break; } q.pop(); for (int i = 0; i < t.size() - 1; i++) { string tmp = t; swap(tmp[i], tmp[i + 1]); if (visit.find(tmp) == visit.end()) { visit[tmp] = visit[t]; q.push(tmp); } } } if (isfind == false)cout << -1 << endl; } int main() { int n; string str; set<string>s; while (cin >> n >> str) { // 注意 while 处理多个 case bfs(str); } } // 64 位输出请用 printf("%lld")