C++#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=113+10;
int sx;
string a;
int fx[]={0,3,-3,1,-1};
struct node{
int x,step;
string s;
};
queue<node>q;
map<string,bool>mp;
void bfs(int x){
q.push({x,0,a});
mp[a]=1;
while(!q.empty()){
node t=q.front();
q.pop();
if(t.s=="123804765"){
cout<<t.step;
return;
}
for(int i=1;i<=4;i++){
int tx=t.x+fx[i];
if((t.x==2||t.x==5||t.x==8)&&i==3)continue;
if((t.x==3||t.x==6)&&i==4)continue;
if(tx>=0&&tx<9){
swap(t.s[t.x],t.s[tx]);
if(mp[t.s]==0){
q.push({tx,t.step+1,t.s});
mp[t.s]=1;
}
swap(t.s[t.x],t.s[tx]);
}
}
}
}
int main(){
ios::sync_with_stdio(0);
cin>>a;
for(int i=0;i<9;i++){
if(a[i]=='0'){
sx=i;
break;
}
}
bfs(sx);
return 0;
}