#include<iostream> #include<vector> #include<climits> #include<string> #include<queue> #include<functional> #include<set> #include<algorithm> #include<map> usingnamespace std; #define il inline #define min(a,b) (a<b?a:b) #define max(a,b) (a>b?a:b) #define MAX ((int)1e9) structnode { int map[3][3]; int x, y, val; node(int ma[3][3], int x, int y, int val) :x(x), y(y), val(val) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) map[i][j] = ma[i][j]; } }; int dr[][2]{ 1,0,0,1,-1,0,0,-1 }; int ans[3][3]{ 1,2,3,8,0,4,7,6,5 }; il boolcheck(constint map[3][3]) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (map[i][j] != ans[i][j]) returnfalse; returntrue; } il intget(int map[3][3]) { int ans = 0, bs = 1; for (int i = 2; i >= 0; i--) for (int j = 2; j >= 0; j--) { ans += bs * map[i][j]; bs *= 10; } return ans; } map<int,bool> vis; intmain() { int mapp[3][3]; int g; cin >> g; int x, y; for (int i = 2; i >= 0; i--) for (int j = 2; j >= 0; j--) { mapp[i][j] = g % 10; g /= 10; if (mapp[i][j] == 0) x = i, y = j; } queue<node> Q; Q.push({ mapp,x,y,0 }); while (!Q.empty()) { node noww = Q.front(); Q.pop(); if (vis[get(noww.map)])continue; vis[get(noww.map)] = true; if (check(noww.map)) { cout << noww.val; return0; } for (int k = 0; k < 4; k++) { node now = noww; x = now.x + dr[k][0], y = now.y + dr[k][1]; if (x >= 0 && x <= 2 && y >= 0 && y <= 2) { int t = now.map[x][y]; now.map[now.x][now.y] = t; now.map[x][y] = 0; now.x = x, now.y = y; now.val++; if (!vis[get(now.map)]) Q.push(now); } } } return0; }