# -*- coding: utf-8 -*-

#// left rotate
def rotate1L(seq):
	seq = list(seq)
	seq[1],seq[5],seq[4]=seq[5],seq[4],seq[1]
	return tuple(seq)
def rotate2L(seq):
	seq = list(seq)
	seq[1],seq[2],seq[6],seq[5],seq[4]=seq[2],seq[6],seq[5],seq[4],seq[1]
	return tuple(seq)
def rotate3L(seq):
	seq = list(seq)
	seq[1],seq[2],seq[3],seq[7],seq[6],seq[5],seq[4]=seq[2],seq[3],seq[7],seq[6],seq[5],seq[4],seq[1]
	return tuple(seq)
#// right rotate
def rotate1R(seq):
	seq = list(seq)
	seq[1],seq[5],seq[4]=seq[4],seq[1],seq[5]
	return tuple(seq)
def rotate2R(seq):
	seq = list(seq)
	seq[1],seq[2],seq[6],seq[5],seq[4]=seq[4],seq[1],seq[2],seq[6],seq[5]
	return tuple(seq)
def rotate3R(seq):
	seq = list(seq)
	seq[1],seq[2],seq[3],seq[7],seq[6],seq[5],seq[4]=seq[4],seq[1],seq[2],seq[3],seq[7],seq[6],seq[5]
	return tuple(seq)

def gen_new(seq,d):
	yield rotate1L(seq),d+4
	yield rotate2L(seq),d+6
	yield rotate3L(seq),d+8
	yield rotate1R(seq),d+4
	yield rotate2R(seq),d+6
	yield rotate3R(seq),d+8

from collections import defaultdict
step = defaultdict(int)
def init_step():
	global step
	seq = tuple(list(range(8)))
	current = set()
	current.add(seq)
	step[seq]=0
	while len(current)!=0:
		candidate = set()
		for s in current:
			for ss,d in gen_new(s,step[s]):
				if ss not in step:
					candidate.add(ss)
					step[ss]=d
					continue
				if d < step[ss]:
					step[ss]=d
					candidate.add(ss)
		current = candidate

def solve(seq):
	init_step = 0
	# let CACHE[0]==0 and calculate init step
	zero_position = seq.index(0)
	col,row = zero_position%4,zero_position//4
	if row==1:
		init_step += 1
		seq[col],seq[col+4]=seq[col+4],seq[col]
	while col!=0:
		init_step += 1
		seq[col],seq[col-1]=seq[col-1],seq[col]
		col -= 1
	global step
	return step[tuple(seq)]+init_step


def main():
	init_step()
	line = input().strip()
	while line!='':
		seq = list(map(int,line.split(' ')))
		print(solve(seq))
		line = input().strip()

if __name__=='__main__':
	main()

"""

/*

0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0


0
1
28
*/

"""

### FIXED CODE ###

from __future__ import division
import sys
if sys.version_info[0]>=3: raw_input=input

X=4
Y=2
i=0
m={}
prev={}
v=[]
for i in range(X*Y): v.append(str(i))
m[''.join(v)]=[0,0]
prev[''.join(v)]=[''.join(v),None]
q=[v]
while len(q)>0:
	v=q.pop(0)
	coor=m[''.join(v)][0]
	x=coor%X
	y=coor//X
	depth=m[''.join(v)][1]
	nextstr=''.join(v)
	if 0<x:
		v[coor],v[coor-1]=v[coor-1],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor-1,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'R']
		v[coor],v[coor-1]=v[coor-1],v[coor]
	if x<X-1:
		v[coor],v[coor+1]=v[coor+1],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor+1,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'L']
		v[coor],v[coor+1]=v[coor+1],v[coor]
	if 0<y:
		v[coor],v[coor-X]=v[coor-X],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor-X,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'D']
		v[coor],v[coor-X]=v[coor-X],v[coor]
	if y<Y-1:
		v[coor],v[coor+X]=v[coor+X],v[coor]
		if ''.join(v) not in m:
			m[''.join(v)]=[coor+X,depth+1]
			q.append(v[:])
			prev[''.join(v)]=[nextstr,'U']
		v[coor],v[coor+X]=v[coor+X],v[coor]

try:
	while True:
		v=raw_input().split()
		print(m[''.join(v)][1])
except EOFError:
	pass