#include<bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

const int W = 3;

struct Point{
    ll x;
    ll y;
    Point(ll x, ll y): x(x), y(y) {}
    Point(){}
};

struct Puzzle{
    ll A[W][W];
    vector<ll> path;

    Point get_space(){
        Point p;
        FOR(y, 0, 3){
            FOR(x, 0, 3){
                if(A[y][x]==0){
                    p.x = x;
                    p.y = y;
                    return p;
                }
            }
        }
        return p;
    }

    bool operator<(const Puzzle &b)const
    {
        FOR(y, 0, 3){
            FOR(x, 0, 3){
                if(A[y][x] != b.A[y][x]){
                    return A[y][x] < b.A[y][x];
                }
            }
        }
        return false;
    }

    bool is_solved(){
        FOR(y, 0, W){
            FOR(x, 0, W){
                ll target = W*y + x + 1;
                if(y==W-1 && x==W-1) target = 0;
                if(A[y][x]!=target) return false;
            }
        }
        return true;
    }

    void print(){
        FOR(y, 0, W){
            FOR(x, 0, W){
                cout << A[y][x] << ' ';
            }
            cout << endl;
        }
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // 初期状態
    auto p = Puzzle();
    FOR(y, 0, 3){
        FOR(x, 0, 3){
            cin >> p.A[y][x];
        }
    }

    // p.print();

    // p.A[0][0] = 1;
    // p.A[0][1] = 2;
    // p.A[0][2] = 3;
    
    // p.A[1][0] = 4;
    // p.A[1][1] = 5;
    // p.A[1][2] = 6;
    
    // p.A[2][0] = 7;
    // p.A[2][1] = 8;
    // p.A[3][2] = 0;

    // pn(p.is_solved());
    // return 0;
    

    map<Puzzle, bool> mp;
    mp[p] = true;

    queue<Puzzle> que;
    que.push(p);

    while(!que.empty()){
        auto pu = que.front();
        que.pop();

        // pu.print();
        // br();

        if(pu.is_solved()){
            p(pu.path.size());
            return 0;
        }

        FOR(i, 0, 4){
            auto sp = pu.get_space();
            ll x = sp.x;
            ll y = sp.y;
            
            // target position
            ll tx = x + dx[i];
            ll ty = y + dy[i];

            auto cp = pu;

            // in range
            if(0<=tx && tx<W && 0<=ty && ty<W){
                swap(cp.A[y][x], cp.A[ty][tx]);

                // まだその状態になったことがない
                if(mp[cp]!=true){
                    mp[cp] = true;
                    cp.path.push_back(i);
                    que.push(cp);
                }   
            }
        }
    }
    
    return 0;
}


Measured memory_bytes: 66056