#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <climits>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
struct node {
int matrix[3][3];
int step, x, y;
int last[2];
};
int ans;
int d_x[4] = {-1, 0, 1, 0};
int d_y[4] = {0, 1, 0, -1};
bool judge(int x, int y) {
return !(x < 0 || x > 2 || y < 0 || y > 2);
}
bool cmp(const node &n1, const node &n2) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (n1.matrix[i][j] != n2.matrix[i][j]) return false;
}
}
return true;
}
node start, dest;
void solve() {
queue<node> q;
q.push(start);
int step = start.step;
while (!q.empty()) {
auto t = q.front();
q.pop();
if (cmp(t, dest)) {
printf("%d\n", t.step);
return;
}
int x = t.x;
int y = t.y;
if (step != t.step) {
step = t.step;
}
for (int i = 0; i < 4; ++i) {
int new_x = x + d_x[i];
int new_y = y + d_y[i];
if (judge(new_x, new_y) && (new_x!= t.last[0] || new_y!=t.last[1])) {
int matrix[3][3];
memcpy(matrix, t.matrix, sizeof(t.matrix));
swap(matrix[t.x][t.y], matrix[new_x][new_y]);
node temp;
temp.x =new_x;
temp.y =new_y;
temp.step = step+1;
temp.last[0] = t.x;
temp.last[1] = t.y;
memcpy(temp.matrix, matrix, sizeof(matrix));
q.push(temp);
}
}
}
}
int main() {
ans = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &start.matrix[i][j]);
if (start.matrix[i][j] == 0) {
start.x = i;
start.y = j;
}
}
}
start.step = 1;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &dest.matrix[i][j]);
}
}
solve();
return 0;
}