import sys

import queue



N = 3

f= ''.join(sys.stdin.readline().split())

f+= ''.join(sys.stdin.readline().split())

f+= ''.join(sys.stdin.readline().split())



dd = [[-1, 0], [0, -1], [1, 0], [0, 1]]



Q = queue.Queue()

V = dict()



Q.put([f,f.index('0'),0])

V[f] = True



while not Q.empty():

    u = Q.get()

    if u[0] == '123456780':  break

    sx, sy = u[1]//N, u[1]%N

    for dx, dy in dd:

        tx, ty = sx+dx, sy+dy

        if tx<0 or ty<0 or tx>=N or ty>=N: continue

        v = u[:]

        n1, n2 = u[1], tx*N+ty

        v[1] = n2

        if n1>n2: n1, n2 = n2, n1

        v[0] = v[0][0:n1]+v[0][n2]+v[0][n1+1:n2]+v[0][n1]+v[0][n2+1:]

        if not V.get(v[0], False):

            V[v[0]] = True

            v[2] += 1                                                    

            Q.put(v)



print((u[2]))

import sys

import queue



N = 3



f = ''.join(sys.stdin.readline().split())

f += ''.join(sys.stdin.readline().split())

f += ''.join(sys.stdin.readline().split())



dd = [[-1, 0], [0, -1], [1, 0], [0, 1]]



Q = [[f,f.index('0'),0]]

V = { f: True }



while len(Q)>0:

    u = Q.pop(0)

    if u[0] == '123456780':  break

    sx, sy = u[1]//N, u[1]%N

    for dx, dy in dd:

        tx, ty = sx+dx, sy+dy

        if tx<0 or ty<0 or tx>=N or ty>=N: continue

        v = u[:]

        n1, n2 = u[1], tx*N+ty

        v[1] = n2

        if n1>n2: n1, n2 = n2, n1

        v[0] = v[0][0:n1]+v[0][n2]+v[0][n1+1:n2]+v[0][n1]+v[0][n2+1:]

        if not V.get(v[0], False):

            V[v[0]] = True

            v[2] += 1 # dir[r]                                                       

            Q.append(v)



print((u[2]))

import sys
import queue

N = 3
f = "".join(sys.stdin.readline().split())
f += "".join(sys.stdin.readline().split())
f += "".join(sys.stdin.readline().split())
dd = [[-1, 0], [0, -1], [1, 0], [0, 1]]
Q = queue.Queue()
V = dict()
Q.put([f, f.index("0"), 0])
V[f] = True
while not Q.empty():
    u = Q.get()
    if u[0] == "123456780":
        break
    sx, sy = u[1] // N, u[1] % N
    for dx, dy in dd:
        tx, ty = sx + dx, sy + dy
        if tx < 0 or ty < 0 or tx >= N or ty >= N:
            continue
        v = u[:]
        n1, n2 = u[1], tx * N + ty
        v[1] = n2
        if n1 > n2:
            n1, n2 = n2, n1
        v[0] = v[0][0:n1] + v[0][n2] + v[0][n1 + 1 : n2] + v[0][n1] + v[0][n2 + 1 :]
        if not V.get(v[0], False):
            V[v[0]] = True
            v[2] += 1
            Q.put(v)
print((u[2]))

import sys
import queue

N = 3
f = "".join(sys.stdin.readline().split())
f += "".join(sys.stdin.readline().split())
f += "".join(sys.stdin.readline().split())
dd = [[-1, 0], [0, -1], [1, 0], [0, 1]]
Q = [[f, f.index("0"), 0]]
V = {f: True}
while len(Q) > 0:
    u = Q.pop(0)
    if u[0] == "123456780":
        break
    sx, sy = u[1] // N, u[1] % N
    for dx, dy in dd:
        tx, ty = sx + dx, sy + dy
        if tx < 0 or ty < 0 or tx >= N or ty >= N:
            continue
        v = u[:]
        n1, n2 = u[1], tx * N + ty
        v[1] = n2
        if n1 > n2:
            n1, n2 = n2, n1
        v[0] = v[0][0:n1] + v[0][n2] + v[0][n1 + 1 : n2] + v[0][n1] + v[0][n2 + 1 :]
        if not V.get(v[0], False):
            V[v[0]] = True
            v[2] += 1  # dir[r]
            Q.append(v)
print((u[2]))

-Q = queue.Queue() -V = dict() -Q.put([f, f.index("0"), 0]) -V[f] = True -while not Q.empty(): -    u = Q.get() +Q = [[f, f.index("0"), 0]] +V = {f: True} +while len(Q) > 0: +    u = Q.pop(0) -            v[2] += 1 -            Q.put(v) +            v[2] += 1  # dir[r] +            Q.append(v)