move = [[1,4],[0,2,5],[1,3,6],[2,7],[0,5],[4,1,6],[5,2,7],[6,3]]



def swap(ls,p,q):

    res = list(ls)

    res[p],res[q] = res[q],res[p]

    return "".join(res)



ans = {"01234567":0}

que = [["01234567",0]]

while que:

    pzl,count = que.pop(0)

    p = pzl.find("0")

    for q in move[p]:

        tmp = swap(pzl,p,q)

        if tmp not in ans:

            ans[tmp] = count+1

            que.append([tmp,count+1])



while 1:

    try:

        print(ans[input().replace(" ","")])

    except:

        break
move = [[1,4],[0,2,5],[1,3,6],[2,7],[0,5],[4,1,6],[5,2,7],[6,3]]



ans = {"01234567":0}

que = [["01234567",0]]

while que:

    pzl,count = que.pop(0)

    p = pzl.find("0")

    for q in move[p]:

        a,b = min(p,q),max(p,q)

        tmp = pzl[:a]+pzl[b]+pzl[a+1:b]+pzl[a]+pzl[b+1:]

        if tmp not in ans:

            ans[tmp] = count+1

            que.append([tmp,count+1])



while 1:

    try:

        print(ans[input().replace(" ","")])

    except:

        break
move = [[1, 4], [0, 2, 5], [1, 3, 6], [2, 7], [0, 5], [4, 1, 6], [5, 2, 7], [6, 3]]


def swap(ls, p, q):
    res = list(ls)
    res[p], res[q] = res[q], res[p]
    return "".join(res)


ans = {"01234567": 0}
que = [["01234567", 0]]
while que:
    pzl, count = que.pop(0)
    p = pzl.find("0")
    for q in move[p]:
        tmp = swap(pzl, p, q)
        if tmp not in ans:
            ans[tmp] = count + 1
            que.append([tmp, count + 1])
while 1:
    try:
        print(ans[input().replace(" ", "")])
    except:
        break

move = [[1, 4], [0, 2, 5], [1, 3, 6], [2, 7], [0, 5], [4, 1, 6], [5, 2, 7], [6, 3]]
ans = {"01234567": 0}
que = [["01234567", 0]]
while que:
    pzl, count = que.pop(0)
    p = pzl.find("0")
    for q in move[p]:
        a, b = min(p, q), max(p, q)
        tmp = pzl[:a] + pzl[b] + pzl[a + 1 : b] + pzl[a] + pzl[b + 1 :]
        if tmp not in ans:
            ans[tmp] = count + 1
            que.append([tmp, count + 1])
while 1:
    try:
        print(ans[input().replace(" ", "")])
    except:
        break

- - -def swap(ls, p, q): -    res = list(ls) -    res[p], res[q] = res[q], res[p] -    return "".join(res) - - -        tmp = swap(pzl, p, q) +        a, b = min(p, q), max(p, q) +        tmp = pzl[:a] + pzl[b] + pzl[a + 1 : b] + pzl[a] + pzl[b + 1 :]