#include<bits/stdc++.h>
const int LEN = 362880;
using namespace std;
struct node{
int state[9];
int dis;
};
int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};
int visited[LEN] = {0};
int start[9];
int goal[9];
long int factory[] = {1,1,2,6,24,120,720,5040,40320,362880};
int Cantor(int str[],int n){
long result = 0;
for(int i = 0; i < n; i++){
int counted = 0;
for(int j = i+1;j < n; j++){
if(str[i]>str[j]){
counted++;
}
}
result+=counted * factory[n-i-1];
}
if(!visited[result]){
visited[result] = 1;
return 1;
}else{
return 0;
}
}
int bfs(){
node head;
memcpy(head.state,start,sizeof(head.state));
head.dis = 0;
queue<node> q;
Cantor(head.state,9);
q.push(head);
while(!q.empty()){
head = q.front();
q.pop();
if(memcmp(head.state,goal,sizeof(goal))==0){
return head.dis;
}
int z;
for(z = 0;z < 9;z++){
if(head.state[z]==0){
break;
}
}
int y = z %3;
int x = z / 3;
for(int i = 0; i < 4;i++){
int newX = x+dir[i][0];
int newY = y + dir[i][1];
int nz = newX*3+newY;
if(newX>=0&&newX<3&&newY>=0&&newY<3){
node newnode;
memcpy(&newnode,&head,sizeof(struct node));
swap(newnode.state[z],newnode.state[nz]);
newnode.dis++;
if(Cantor(newnode.state,9)){
q.push(newnode);
}
}
}
}
return -1;
}
int main()
{
for(int i = 0;i <9;i++){
cin>>start[i];
}
for(int i = 0;i <9;i++){
cin>>goal[i];
}
int num = bfs();
cout<<num<<endl;
return 0;
}