from collections import deque

from copy import deepcopy, copy

dy = [-1, 0, 0, 1]

dx = [0, -1, 1, 0]

N = 3

def g(i, j, a):

    t = a // (10 ** j) % 10

    return a - t * (10 ** j) + t * (10 ** i)

def MAIN():

    m = {8:{7, 5}, 7:{8, 6, 4}, 6:{7, 3}, 5:{8, 4, 2}, 4:{7, 5, 3, 1}, 3:{6, 4, 0}, 2:{5, 1}, 1:{4, 2, 0}, 0:{3, 1}}

    MAP = "".join(input().replace(" ", "") for _ in range(N))

    start = 8 - MAP.find("0")

    MAP = int(MAP)

    goal = ('1', '2', '3', '4', '5', '6', '7', '8', '0')

    goal = 123456780

    dp = deque([(0, start, MAP)])

    LOG = {MAP}

    while dp:

        cnt, yx, M = dp.popleft()

        if M == goal:

            print(cnt)

            break

        cnt += 1

        for nyx in m[yx]:

            CM = g(yx, nyx, M)

            if not CM in LOG:

                dp.append((cnt, nyx, CM))

                LOG.add(CM)

MAIN()



from collections import deque

from copy import deepcopy, copy

N = 3

def g(i, j, a):

    if i > j:

        i, j = j, i

    return a[:i] + a[j] + a[i + 1:j] + a[i] + a[j + 1:]

def MAIN():

    m = {8:{7, 5}, 7:{8, 6, 4}, 6:{7, 3}, 5:{8, 4, 2}, 4:{7, 5, 3, 1}, 3:{6, 4, 0}, 2:{5, 1}, 1:{4, 2, 0}, 0:{3, 1}}

    MAP = "".join(input().replace(" ", "") for _ in range(N))

    start = MAP.find("0")

    goal = "123456780"

    dp = deque([(0, start, MAP)])

    LOG = {MAP}

    while dp:

        cnt, yx, M = dp.popleft()

        if M == goal:

            print(cnt)

            break

        cnt += 1

        for nyx in m[yx]:

            CM = g(yx, nyx, M)

            if not CM in LOG:

                dp.append((cnt, nyx, CM))

                LOG.add(CM)

MAIN()



from collections import deque
from copy import deepcopy, copy

dy = [-1, 0, 0, 1]
dx = [0, -1, 1, 0]
N = 3


def g(i, j, a):
    t = a // (10**j) % 10
    return a - t * (10**j) + t * (10**i)


def MAIN():
    m = {
        8: {7, 5},
        7: {8, 6, 4},
        6: {7, 3},
        5: {8, 4, 2},
        4: {7, 5, 3, 1},
        3: {6, 4, 0},
        2: {5, 1},
        1: {4, 2, 0},
        0: {3, 1},
    }
    MAP = "".join(input().replace(" ", "") for _ in range(N))
    start = 8 - MAP.find("0")
    MAP = int(MAP)
    goal = ("1", "2", "3", "4", "5", "6", "7", "8", "0")
    goal = 123456780
    dp = deque([(0, start, MAP)])
    LOG = {MAP}
    while dp:
        cnt, yx, M = dp.popleft()
        if M == goal:
            print(cnt)
            break
        cnt += 1
        for nyx in m[yx]:
            CM = g(yx, nyx, M)
            if not CM in LOG:
                dp.append((cnt, nyx, CM))
                LOG.add(CM)


MAIN()

from collections import deque
from copy import deepcopy, copy

N = 3


def g(i, j, a):
    if i > j:
        i, j = j, i
    return a[:i] + a[j] + a[i + 1 : j] + a[i] + a[j + 1 :]


def MAIN():
    m = {
        8: {7, 5},
        7: {8, 6, 4},
        6: {7, 3},
        5: {8, 4, 2},
        4: {7, 5, 3, 1},
        3: {6, 4, 0},
        2: {5, 1},
        1: {4, 2, 0},
        0: {3, 1},
    }
    MAP = "".join(input().replace(" ", "") for _ in range(N))
    start = MAP.find("0")
    goal = "123456780"
    dp = deque([(0, start, MAP)])
    LOG = {MAP}
    while dp:
        cnt, yx, M = dp.popleft()
        if M == goal:
            print(cnt)
            break
        cnt += 1
        for nyx in m[yx]:
            CM = g(yx, nyx, M)
            if not CM in LOG:
                dp.append((cnt, nyx, CM))
                LOG.add(CM)


MAIN()

-dy = [-1, 0, 0, 1] -dx = [0, -1, 1, 0] -    t = a // (10**j) % 10 -    return a - t * (10**j) + t * (10**i) +    if i > j: +        i, j = j, i +    return a[:i] + a[j] + a[i + 1 : j] + a[i] + a[j + 1 :] -    start = 8 - MAP.find("0") -    MAP = int(MAP) -    goal = ("1", "2", "3", "4", "5", "6", "7", "8", "0") -    goal = 123456780 +    start = MAP.find("0") +    goal = "123456780"