#include <bits/stdc++.h>
#include <cmath>

using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
//typedef long long ll;
typedef long long ll;
//typedef pair<int, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
const ll INF = 10e15;
const ll MINF = -10e10;
//const int INF = INT_MAX / 100;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)


//ifstream myfile("~/Downloads/02.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;
const int mod = 1000000007;
const double PI = 3.14159265358979323846;

typedef pair<ll, ll> P;


string to_upper(string str) {
    int diff = 'a' - 'A';
    for (int i = 0; i < str.size(); i++) {
        char c = str[i];
        if ('A' <= c && c <= 'Z') {
            c += diff;
            str[i] = c;
        }
    }
    return str;
}

vector<int> up(vector<int> &board) {
    auto it = find(board.begin(), board.end(), 0);
    int id = distance(board.begin(), it);
    if (id == 0 || id == 1 || id == 2) {
        return vector<int>(0);
    }
    vector<int> res = board;
    swap(res[id], res[id - 3]);
    return res;
}

vector<int> down(vector<int> &board) {
    auto it = find(board.begin(), board.end(), 0);
    int id = distance(board.begin(), it);
    if (id == 6 || id == 7 || id == 8) {
        return vector<int>(0);
    }
    vector<int> res = board;
    swap(res[id], res[id + 3]);
    return res;
}

vector<int> right(vector<int> &board) {
    auto it = find(board.begin(), board.end(), 0);
    int id = distance(board.begin(), it);
    if (id == 2 || id == 5 || id == 8) {
        return vector<int>(0);
    }
    vector<int> res = board;
    swap(res[id], res[id + 1]);
    return res;
}

vector<int> left(vector<int> &board) {
    auto it = find(board.begin(), board.end(), 0);
    int id = distance(board.begin(), it);
    if (id == 0 || id == 3 || id == 6) {
        return vector<int>(0);
    }
    vector<int> res = board;
    swap(res[id], res[id - 1]);
    return res;
}

struct Board {
    vector<int> board;
    int depth;
};

int bfs(vector<int> &board) {

    vector<int> clear = {1, 2, 3, 4, 5, 6, 7, 8, 0};
    set<vector<int> > used;

    queue<Board> q;
    q.push({board, 0});

    while (!q.empty()) {
        Board top = q.front();
        q.pop();
        if (top.board == clear) {
            return top.depth;
        }
        if (used.find(top.board) != used.end()) {
            continue;
        }
        used.insert(top.board);

        vector<int> u = up(top.board),
                d = down(top.board),
                r = right(top.board),
                l = left(top.board);

        if (!u.empty()) {
            q.push({u, top.depth + 1});
        }
        if (!d.empty()) {
            q.push({d, top.depth + 1});
        }
        if (!r.empty()) {
            q.push({r, top.depth + 1});
        }
        if (!l.empty()) {
            q.push({l, top.depth + 1});
        }
    }

    return -1;
}

int main() {
    vector<int> board(9, 0);
    rep(i, 9) cin >> board[i];

    int ans = bfs(board);

    cout << ans << endl;

}


Measured memory_bytes: 24944