# Original solution
import sys;
import heapq

def iterative(i,j):
    q = []
    q.append((simpleHS(puz),0,i,j,0,puz))
    global finding

    while len(q):
        items = q.pop()
        c_dWithH = items[0]
        c_depth = items[1]
        _i = items[2]
        _j = items[3]
        prev_move = items[4]
        c_puz = items[5]
        if(puzzleCheck(c_puz)):
            finding = 1
            print(c_depth)
            break
        if((c_depth+c_dWithH) > depth):
            continue
            
        if(_i != 0 and prev_move != 1):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i-1,_j)
            heapq.heappush(q,(c_depth+1+simpleHS(swap_puz),c_depth+1,_i-1,_j,2,swap_puz))
        if(_i != 2 and prev_move != 2):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i+1,_j)
            heapq.heappush(q,(c_depth+1+simpleHS(swap_puz),c_depth+1,_i+1,_j,1,swap_puz))
        if(_j != 0 and prev_move != 3):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i,_j-1)
            heapq.heappush(q,(c_depth+1+simpleHS(swap_puz),c_depth+1,_i,_j-1,4,swap_puz))
        if(_j != 2 and prev_move != 4):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i,_j+1)
            heapq.heappush(q,(c_depth+1+simpleHS(swap_puz),c_depth+1,_i,_j+1,3,swap_puz))

def checkPuzIsExisted(puz,q):
    for i in range(len(q)):
        exist_puz = q[i][4]
        count = 0
        for j in range(len(exist_puz)):
            if(exist_puz[j] != puz[j]):
                count = 1
        if(count == 0):
            return True
    return False

def sumCost(costs):
    value = 0
    for i in range(len(costs)):
        value += costs[i]
    return value

def simpleHS(c_puz):
    count = 0
    for i in range(len(correctPuz)):
        if(correctPuz[i] != c_puz[i]):
            count+=1
    return count

def HS(i,j,num):
    k = correctPuz.index(num)
    ki = (int)(k/3)
    kj = k - ki*3
    value = abs(_i-ki)+abs(_j-kj) 
    return value


def swapPuz(c_puz, i, j, i2,j2):
    c_puz[i2*3+j2],c_puz[i*3+j] = c_puz[i*3+j],c_puz[i2*3+j2]
    return c_puz

def puzzleCheck(c_puz):
    if(c_puz[8] == 0):
        for i in range(len(correctPuz)):
            if(correctPuz[i] != c_puz[i]):
                return False
        return True
    return False

correctPuz = [i+1 for i in range(9)]
correctPuz[8] = 0
puz = [0 for i in range(9)]

i_start = 0
j_start = 0

for i in range(3):
    puz[i*3],puz[i*3+1],puz[i*3+2] = list(map(int, input().split()));
    if(puz[i*3] == 0):
        i_start,j_start = i,0
    elif(puz[i*3+1] == 0):
        i_start,j_start = i,1
    elif(puz[i*3+2] == 0):
        i_start,j_start = i,2

finding = 0
depth = 0
while True:
    if(finding == 1):
        break
    iterative(i_start,j_start)
    depth+=1



# Optimized solution
import sys;
import heapq

def iterative(i,j):
    q = []
    q.append((simpleHS(puz),0,i,j,0,puz))
    global finding

    while len(q):
        items = q.pop(0)
        c_dWithH = items[0]
        c_depth = items[1]
        _i = items[2]
        _j = items[3]
        prev_move = items[4]
        c_puz = items[5]
        if(puzzleCheck(c_puz)):
            finding = 1
            print(c_depth)
            break
        if((c_depth+c_dWithH) > depth):
            continue

        if(_i != 0 and prev_move != 1):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i-1,_j)
            q.append((c_depth+1+simpleHS(swap_puz),c_depth+1,_i-1,_j,2,swap_puz))
        if(_i != 2 and prev_move != 2):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i+1,_j)
            q.append((c_depth+1+simpleHS(swap_puz),c_depth+1,_i+1,_j,1,swap_puz))
        if(_j != 0 and prev_move != 3):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i,_j-1)
            q.append((c_depth+1+simpleHS(swap_puz),c_depth+1,_i,_j-1,4,swap_puz))
        if(_j != 2 and prev_move != 4):
            swap_puz = swapPuz(c_puz[0:],_i,_j,_i,_j+1)
            q.append((c_depth+1+simpleHS(swap_puz),c_depth+1,_i,_j+1,3,swap_puz))


def checkPuzIsExisted(puz,q):
    for i in range(len(q)):
        exist_puz = q[i][4]
        count = 0
        for j in range(len(exist_puz)):
            if(exist_puz[j] != puz[j]):
                count = 1
        if(count == 0):
            return True
    return False

def sumCost(costs):
    value = 0
    for i in range(len(costs)):
        value += costs[i]
    return value

def simpleHS(c_puz):
    count = 0
    for i in range(len(correctPuz)):
        if(correctPuz[i] != c_puz[i]):
            count+=1
    return count

def HS(i,j,num):
    k = correctPuz.index(num)
    ki = (int)(k/3)
    kj = k - ki*3
    value = abs(_i-ki)+abs(_j-kj) 
    return value


def swapPuz(c_puz, i, j, i2,j2):
    c_puz[i2*3+j2],c_puz[i*3+j] = c_puz[i*3+j],c_puz[i2*3+j2]
    return c_puz

def puzzleCheck(c_puz):
    if(c_puz[8] == 0):
        for i in range(len(correctPuz)):
            if(correctPuz[i] != c_puz[i]):
                return False
        return True
    return False

correctPuz = [i+1 for i in range(9)]
correctPuz[8] = 0
puz = [0 for i in range(9)]

i_start = 0
j_start = 0

for i in range(3):
    puz[i*3],puz[i*3+1],puz[i*3+2] = list(map(int, input().split()));
    if(puz[i*3] == 0):
        i_start,j_start = i,0
    elif(puz[i*3+1] == 0):
        i_start,j_start = i,1
    elif(puz[i*3+2] == 0):
        i_start,j_start = i,2

finding = 0
depth = 0
while True:
    if(finding == 1):
        break
    iterative(i_start,j_start)
    depth+=1