// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<map>

#include<queue>

#include<cmath>

using namespace std;



#define N 3

#define N2 9



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

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



int MDT[N2][N2];



struct Puzzle

{

	int f[N2], space, MD;

	int cost;

	bool operator < (const Puzzle &p) const

	{

		for(int i = 0; i < N2; i++){

			if(f[i] == p.f[i]) continue;

			return f[i] > p.f[i];

		}

		return false;

	}

};



int getallMD(const Puzzle &p)

{

	int h = 0;

	for(int i = 0; i < N2; i++){

	if(i == p.space) continue;

		h += MDT[i][p.f[i] - 1];

	}

	return h;

}



struct State

{

	Puzzle pz;

	int estimated;

	bool operator < (const State &s) const

	{

		return estimated > s.estimated;

	}

};



int astar(Puzzle p)

{

	priority_queue<State> A;

	map<Puzzle, bool> USED;



	State initial;

	initial.pz = p;

	initial.estimated = p.MD;

	A.push(initial);

	USED[p] = true;



	Puzzle u, v;

	int sx, sy, tx, ty, r;



	while(!A.empty())

	{

		u = A.top().pz; A.pop();

		if(u.MD == 0) return u.cost;



		sx = u.space / N;

		sy = u.space % N;

		for(r = 0; r < 4; r++){

			tx = sx + dx[r];

			ty = sy + dy[r];

			if(tx < 0 || tx >= N || ty < 0 || ty >= N) continue;

			v = u;

			v.MD -= MDT[tx * N + ty][u.f[tx * N + ty] - 1];

			v.MD += MDT[sx * N + sy][u.f[tx * N + ty] - 1];

			swap(v.f[sx * N + sy], v.f[tx * N + ty]);

			v.space = tx * N + ty;

			

			if(!USED[v]){

				v.cost++;

				USED[v] = true;

				State new_s;

				new_s.pz = v;

				new_s.estimated = v.cost + v.MD;

				A.push(new_s);

			}

		}

	};

	return -1;

}



int main()

{

	

	for(int i = 0; i < N2; i++){

		for(int j = 0; j < N2; j++){

			MDT[i][j] = abs((i / N) - (j / N)) + abs((i % N) - (j % N));

		}

	}



	Puzzle in;



	for(int i = 0; i < N2; i++){

		scanf("%d", &in.f[i]);

		if(in.f[i] == 0) in.space = i;

	}

	in.cost = 0;

	in.MD = getallMD(in);



	cout << astar(in) << endl;



	return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

using namespace std;

#include<cstdio>

#include<cmath>  // use "abs" function.



// non-recursive algorithm.

#define N 3

#define N2 9



int main()

{

    int MDT[N2][N2];   // Manhattan Distance Table.

    int dx[4] = {0, -1, 0, 1};  // move.

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



    int i, j;

    for(i = 0; i < N2; i++){

        for(j = 0; j < N2; j++){

            MDT[i][j] = abs((i / N) - (j / N)) + abs((i % N) - (j % N));

        }

    }

    int pre_f[N2], f[N2], pre_space, space, pre_MD, MD, depth;

    for(i = 0; i < N2; i++){

        scanf("%d", &pre_f[i]);

        if(pre_f[i] == 0) pre_space = i;

    }



    pre_MD = 0;

    for(i = 0; i < N2; i++){

        if(pre_f[i] != 0){

            pre_MD += MDT[i][pre_f[i] - 1];

        }

    }



    int level, sx, sy, tx, ty, diff, trail[101], s, r, rev_sp;



    for(level = pre_MD; level < 100; level++, level++){  // DFS.

        for(i = 0; i < N2; i++) f[i] = pre_f[i];

        space = pre_space;

        MD = pre_MD;

        depth = 0;

        trail[0] = 4;

        s = 0;



        while(depth >= 0 && MD > 0)

        {

            sx = space / N;

            sy = space % N;

            for(r = s; r < 4; r++){

                if((trail[depth] ^ r) == 2) continue;  // go forward.

                tx = sx + dx[r];

                ty = sy + dy[r];



                if(tx < 0 || tx >= N || ty < 0 || ty >= N) continue;

                diff = MDT[sx * N + sy][f[tx * N + ty] - 1] - MDT[tx * N + ty][f[tx * N + ty] - 1];

                if(depth + 1 + MD + diff > level) continue;  // branch cut.



                break;

            }

            if(r < 4){  // next direction.

                swap(f[sx * N + sy], f[tx * N + ty]);

                space = tx * N + ty;

                MD += diff;

                depth++;

                trail[depth] = r;

                s = 0;

            }else{

                // back-track.

                r = trail[depth] ^ 2;

                s = trail[depth] + 1;

                depth--;

                if(depth >= 0){

                    rev_sp = space + N * dx[r] + dy[r];

                    MD += MDT[space][f[rev_sp] - 1] - MDT[rev_sp][f[rev_sp] - 1];

                    swap(f[space], f[rev_sp]);

                    space = rev_sp;

                }

            }

        };

        if(depth >= 0) break;

    }

    printf("%d\n", depth);



    return 0;

}

// Fastest known solution for this problem: