using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ALDS1_13_B2
{
    class Program
    {
        static int[] dx = { -1, 0, 1, 0 };
        static int[] dy = { 0, -1, 0, 1 };
 
        static void Main(string[] args)
        {
            var cnt = 0;
            int[] Puzzle = new int[11];
            for (int i = 0; i < 3; i++)
            {
                var k = scan;
                for (int j = 0; j < 3; j++)
                {
                    Puzzle[cnt++] = k[j];
                    if (k[j] == 0)
                    {
                        Puzzle[cnt - 1] = 9;
                        Puzzle[9] = cnt - 1;
                    } 
                }
            }
            bfs(Puzzle);
             
        }
        static bool checker(int[] a)
        {
        	for(int i = 0; i < 9; i++)
        	{
        		if(a[i] == i+1)continue;
        		return false;
        	}
        	return true;
        }
        static void bfs(int[] s)
        {
            //index 0-10
            var Q = new Queue<string>();
            //index 0-8
            var V = new HashSet<string>();
            
            string m = s[0].ToString(), n;
            for(int i = 1;i < 11; i++)
            {
            	m += " " + s[i].ToString();
            }
            n = m.Substring(0, 17);
            Q.Enqueue(m);
            V.Add(n);
             
            while (Q.Any())
            {
                int[] u = Q.Dequeue().Split().Select(int.Parse).ToArray();
                
                //Console.WriteLine(string.Concat(u));
                if (checker(u))
                {
                    Console.WriteLine(u[10]);
                    return;
                }
                int sx = u[9] / 3;
                int sy = u[9] % 3;
                for (int r = 0; r <= 3; r++)
                {
                    int tx = sx + dx[r];
                    int ty = sy + dy[r];
                    if (tx < 0 || ty < 0 || tx >= 3 || ty >= 3) continue;
 
                    var position = (tx * 3 + ty);
                    var nowspace = u[9];
 
                    swap(u, position, nowspace);
                    u[9] = position;
                    
                    n = u[0].ToString();
                    for(int i = 1; i < 9; i++)
                    {
                    	n += " " + u[i].ToString();
                    }
                    if (!V.Contains(n))
                    {
                        V.Add(n);
                        u[10]++;
                        for(int j = 9; j < 11; j++)
                        {
                        	n += " " +u[j].ToString();
                        }
                        Q.Enqueue(n);
                        u[10]--;
                    }
                    u[9] = nowspace;
                    swap(u, position, nowspace);
 
                }
                //Console.WriteLine(string.Join(" & ",Q));
            }
            Console.WriteLine("WA");
        }
        static void swap(int[] s, int x, int y)
        {
            var t = s[x];
            s[x] = s[y];
            s[y] = t;
        }
        static int[] scan { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } }
    }
}

Measured memory_bytes: 40276