# AOJ 0121 Seven Puzzle 

# Python 2017.6.23 bal4u



def swap(state, x, y):

	s = list(copy.deepcopy(state))

	s[x], s[y] = s[y], s[x]

	return tuple(s)

	

from collections import deque

import copy

MAGIC = 37

move = ((1,4), (0,2,5), (1,3,6), (2,7), (0,5), (1,4,6), (2,5,7), (3,6))



Q = deque()

Q.append(((0,1,2,3,4,5,6,7), 0))

hash = {}

hash[(0,1,2,3,4,5,6,7)] = 0

while Q:

	state, x = Q.popleft()

	step = hash[state]+1

	if step > MAGIC: continue

	for y in move[x]:

		nstate = swap(state, x, y)

		if nstate not in hash: nstep = 100

		else: nstep = hash[nstate]

		if step < nstep:

			Q.append((nstate, y))

			hash[nstate] = step



while 1:

	try: state = tuple(map(int, input().split()))

	except: break

	print((hash[state]))

# AOJ 0121 Seven Puzzle 

# Python 2017.6.23 bal4u



def swap(state, x, y):

	s = list(state)

	s[x], s[y] = s[y], s[x]

	return tuple(s)

	

from collections import deque

MAGIC = 37

move = ((1,4), (0,2,5), (1,3,6), (2,7), (0,5), (1,4,6), (2,5,7), (3,6))



Q = deque()

Q.append(((0,1,2,3,4,5,6,7), 0))

hash = {}

hash[(0,1,2,3,4,5,6,7)] = 0

while Q:

	state, x = Q.popleft()

	step = hash[state]+1

	if step > MAGIC: continue

	for y in move[x]:

		nstate = swap(state, x, y)

		if nstate not in hash: nstep = 100

		else: nstep = hash[nstate]

		if step < nstep:

			Q.append((nstate, y))

			hash[nstate] = step



while 1:

	try: state = tuple(map(int, input().split()))

	except: break

	print((hash[state]))

# AOJ 0121 Seven Puzzle
# Python 2017.6.23 bal4u
def swap(state, x, y):
    s = list(copy.deepcopy(state))
    s[x], s[y] = s[y], s[x]
    return tuple(s)


from collections import deque
import copy

MAGIC = 37
move = ((1, 4), (0, 2, 5), (1, 3, 6), (2, 7), (0, 5), (1, 4, 6), (2, 5, 7), (3, 6))
Q = deque()
Q.append(((0, 1, 2, 3, 4, 5, 6, 7), 0))
hash = {}
hash[(0, 1, 2, 3, 4, 5, 6, 7)] = 0
while Q:
    state, x = Q.popleft()
    step = hash[state] + 1
    if step > MAGIC:
        continue
    for y in move[x]:
        nstate = swap(state, x, y)
        if nstate not in hash:
            nstep = 100
        else:
            nstep = hash[nstate]
        if step < nstep:
            Q.append((nstate, y))
            hash[nstate] = step
while 1:
    try:
        state = tuple(map(int, input().split()))
    except:
        break
    print((hash[state]))

# AOJ 0121 Seven Puzzle
# Python 2017.6.23 bal4u
def swap(state, x, y):
    s = list(state)
    s[x], s[y] = s[y], s[x]
    return tuple(s)


from collections import deque

MAGIC = 37
move = ((1, 4), (0, 2, 5), (1, 3, 6), (2, 7), (0, 5), (1, 4, 6), (2, 5, 7), (3, 6))
Q = deque()
Q.append(((0, 1, 2, 3, 4, 5, 6, 7), 0))
hash = {}
hash[(0, 1, 2, 3, 4, 5, 6, 7)] = 0
while Q:
    state, x = Q.popleft()
    step = hash[state] + 1
    if step > MAGIC:
        continue
    for y in move[x]:
        nstate = swap(state, x, y)
        if nstate not in hash:
            nstep = 100
        else:
            nstep = hash[nstate]
        if step < nstep:
            Q.append((nstate, y))
            hash[nstate] = step
while 1:
    try:
        state = tuple(map(int, input().split()))
    except:
        break
    print((hash[state]))

-    s = list(copy.deepcopy(state)) +    s = list(state) -import copy