(通过老师的测试点知道部分多了1)
#include<bits/stdc++.h>
using namespace std;
map<string,bool> l;
string que[1000000];
short qu[1000000];
short sh[1000000];
int a0(string s){
for(int i=0;i<9;i++)
if(s[i]=='0')return i;
return 0;
}
int main(){
// freopen("tai.in","r",stdin);
// freopen("tai.out","w",stdout);
string s;
cin>>s;
l[s]=1;
que[0]=s;
qu[0]=a0(s);
int head=0,tail=0;
sh[0]=0;
while(head<=tail){
// cout<<que[head]<<endl;
// getchar();
if(que[head]=="123804765"){
cout<<sh[head];
return 0;
}
int f=qu[head];
if(f%3!=2){//向右
swap(que[head][f],que[head][f+1]);
if(!l[que[head]])tail++,que[tail]=que[head],qu[tail]=f+1,l[que[head]]=1;
swap(que[head][f],que[head][f+1]);
sh[tail]=sh[head]+1;
}
if(f%3){//向左
swap(que[head][f],que[head][f-1]);
if(!l[que[head]])tail++,que[tail]=que[head],qu[tail]=f-1,l[que[head]]=1;
swap(que[head][f],que[head][f-1]);
sh[tail]=sh[head]+1;
}
if(f>2){//向上
swap(que[head][f],que[head][f-3]);
if(!l[que[head]])tail++,que[tail]=que[head],qu[tail]=f-3,l[que[head]]=1;
swap(que[head][f],que[head][f-3]);
sh[tail]=sh[head]+1;
}
if(f<6){//向下
swap(que[head][f],que[head][f+3]);
if(!l[que[head]])tail++,que[tail]=que[head],qu[tail]=f+3,l[que[head]]=1;
swap(que[head][f],que[head][f+3]);
sh[tail]=sh[head]+1;
}
head++;
}
}