#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
string startt;
string endd = "12345678x";
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs() {
queue<string> q;
unordered_map<string, int> d;
d[startt] = 0;
q.push(startt);
while(q.size()) {
string t = q.front();
q.pop();
int distance = d[t];
if(t == endd) {
return distance;
}
int k = t.find('x');
int x = k / 3, y = k % 3;
for(int i = 0; i < 4; i++) {
int a = x + dx[i], b = y + dy[i];
if(a >= 0 && a < 3 && b >= 0 && b < 3) {
swap(t[k], t[a*3 + b]);
if(!d.count(t)) {
q.push(t);
d[t] = distance + 1;
}
swap(t[k], t[a*3 + b]);
}
}
}
return -1;
}
int main() {
for(int i = 0; i < 9; i++) {
char c;
cin >> c;
startt += c;
}
cout << bfs() << endl;
return 0;
}