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

void debug() { cerr << "\n"; }
template <class T> void debug(const T &x) { cerr << x << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
    for(const T &x : v) {
        cerr << x << " ";
    }
    cerr << "\n";
}

//----------------------------------------------

const int N = 3;
array<array<int, N * N>, N * N> MHTdist;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct State {
    array<char, N * N> data;
    int space_id;
    int d;
    State() {}
    void init() {
        for(int i = 0; i < N * N; i++) {
            if(data[i] == 0) {
                data[i] = N * N;
                space_id = i;
                break;
            }
        }
        d = 0;
    }
    bool operator<(const State &s) const {
        for(int i = 0; i < N * N; i++) {
            if(data[i] != s.data[i]) {
                return (data[i] < s.data[i]);
            }
        }
        return false;
    }
    int h() {
        int res = 0;
        for(int i = 0; i < N * N; i++) {
            if(data[i] == N * N) {
                continue;
            }
            res += MHTdist[i][data[i] - 1];
        }
        return res;
    }
};

struct Data {
    State state;
    int cost;
    bool operator<(const Data &d) const { return (cost > d.cost); }
};

void debugState(const State &s) {
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            cerr << s.data[i * N + j] << " ";
        }
        cerr << "\n";
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    State start;
    {
        for(int i = 0; i < N * N; i++) {
            int hoge;
            cin >> hoge;
            start.data[i] = hoge;
        }
        start.init();
        for(int i = 0; i < N * N; i++) {
            for(int j = 0; j < N * N; j++) {
                MHTdist[i][j] = abs(i / N - j / N) + abs(i % N - j % N);
            }
        }
    }

    map<State, int> dp;
    priority_queue<Data> que;
    que.emplace(Data{start, start.h()});
    dp[start] = 0;

    while(que.size()) {
        auto nowState = que.top().state;
        int nowCost = que.top().cost;
        que.pop();
        // debugState(nowState);
        if(!nowState.h()) {
            cout << nowState.d << "\n";
            return 0;
        }
        int i = nowState.space_id / N;
        int j = nowState.space_id % N;
        for(int k = 0; k < 4; k++) {
            int ni = i + dy[k], nj = j + dx[k];
            if(ni < 0 || ni >= N || nj < 0 || nj >= N) {
                continue;
            }
            State nextState = nowState;
            swap(nextState.data[nowState.space_id],
                 nextState.data[ni * N + nj]);
            nextState.space_id = ni * N + nj;
            if(!dp.count(nextState)) {
                nextState.d++;
                dp[nextState] = nextState.d;
                que.emplace(Data{nextState, nextState.d + nextState.h()});
            } else if(dp[nextState] > nowState.d + 1) {
                dp[nextState] = nowState.d + 1;
                nextState.d = nowState.d + 1;
                que.emplace(Data{nextState, nextState.d + nextState.h()});
            }
        }
    }
}



Measured memory_bytes: 4204