Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import copy,sys

def solve(pieace):
	history,frontier = set(),set()
	frontier.add(tuple(pieace))
	target_list = list(range(8))

	step = 0
	while True:
		next_frontier = set()
		for pieace in frontier:
			if all(x==y for x,y in zip(pieace,target_list)):
#				print([(x,y) for x,y in zip(pieace,target_list)])
				return step
			zero_position = pieace.index(0)
			relative = zero_position%4

			new_peiace = list(copy.deepcopy(pieace))
			new_peiace[relative],new_peiace[relative+4] = new_peiace[relative+4],new_peiace[relative]
			new_peiace = tuple(new_peiace)
			if new_peiace not in history:
				next_frontier.add(new_peiace)

			if relative in [0,1,2]:
				new_peiace = list(copy.deepcopy(pieace))
				new_peiace[zero_position],new_peiace[zero_position+1] = new_peiace[zero_position+1],new_peiace[zero_position]
				new_peiace = tuple(new_peiace)
				if new_peiace not in history:
					next_frontier.add(new_peiace)
			if relative in [1,2,3]:
				new_peiace = list(copy.deepcopy(pieace))
				new_peiace[zero_position],new_peiace[zero_position-1] = new_peiace[zero_position-1],new_peiace[zero_position]
				new_peiace = tuple(new_peiace)
				if new_peiace not in history:
					next_frontier.add(new_peiace)
		history |= frontier
		frontier = next_frontier
		step += 1

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

if __name__=='__main__':
	main()

Fixed submission:
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

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import copy,sys

def solve(pieace):
	target_list = list(range(8))

	frontier,previous_frontier = set(),set()
	frontier.add(tuple(pieace))

	for step in range(10*10):
		next_frontier = set()
		for pieace in frontier:
			if all(x==y for x,y in zip(pieace,target_list)):	return step
			zero_position = pieace.index(0)
			relative = zero_position%4

			new_peiace = list(copy.deepcopy(pieace))
			new_peiace[relative],new_peiace[relative+4] = new_peiace[relative+4],new_peiace[relative]
			new_peiace = tuple(new_peiace)
			if new_peiace not in previous_frontier:
				next_frontier.add(new_peiace)

			if relative in [0,1,2]:
				new_peiace = list(copy.deepcopy(pieace))
				new_peiace[zero_position],new_peiace[zero_position+1] = new_peiace[zero_position+1],new_peiace[zero_position]
				new_peiace = tuple(new_peiace)
				if new_peiace not in previous_frontier:
					next_frontier.add(new_peiace)

			if relative in [1,2,3]:
				new_peiace = list(copy.deepcopy(pieace))
				new_peiace[zero_position],new_peiace[zero_position-1] = new_peiace[zero_position-1],new_peiace[zero_position]
				new_peiace = tuple(new_peiace)
				if new_peiace not in previous_frontier:
					next_frontier.add(new_peiace)

		previous_frontier = frontier
		frontier = next_frontier

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

if __name__=='__main__':
	main()

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- 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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- 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):
	global step
	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
	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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

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)
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):
	global step

	init_step = 0
	# let CACHE[0]==0 and calculate initilizing step size
	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
	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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

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)
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):
	global step

	init_step = 0
	# let CACHE[0]==0 and calculate initilizing step size
	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
	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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

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)
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):
	"""
	global step

	init_step = 0
	# let CACHE[0]==0 and calculate initilizing step size
	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
	return step[tuple(seq)]+init_step
	"""
	return 0


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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

"""

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)
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):
	global step

	init_step = 0
	# let CACHE[0]==0 and calculate initilizing step size
	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
	return step[tuple(seq)]+init_step
	return 0
"""

def main():
	init_step()
	line = input().strip()
	while line!='':
		seq = list(map(int,line.split(' ')))
#		print(solve(seq))
		print(0)
		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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

"""

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)
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):
	global step

	init_step = 0
	# let CACHE[0]==0 and calculate initilizing step size
	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
	return step[tuple(seq)]+init_step
	return 0
"""

def main():
	init_step()
	line = input().strip()
	while line!='':
		seq = list(map(int,line.split(' ')))
#		print(solve(seq))
		print(0)
		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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

"""

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)
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):
	global step

	init_step = 0
	# let CACHE[0]==0 and calculate initilizing step size
	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
	return step[tuple(seq)]+init_step
	return 0
"""

def main():
#	init_step()
	line = input().strip()
	while line!='':
		seq = list(map(int,line.split(' ')))
#		print(solve(seq))
		print(0)
		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 submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

def main():
	line = input().strip()
	while line!='':
		print(0)
		line = input().strip()

if __name__=='__main__':
	main()

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

if __name__=='__main__':
	line = input().strip()
	while line!='':
		print(0)
		line = input().strip()

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

line = input().strip()
while line!='':
	print(0)
	line = input().strip()

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
line = input().strip()
while line!='':
	print(0)
	line = input().strip()

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

def main():
	line = input().strip()
	while line!='':
		seq = list(map(int,line.split(' ')))
		print(0)
		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 submission:
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

Buggy python submission (verdict: Time Limit Exceeded):
while True:
	try:
		input()
		print(0)
	except EOFError:
		 pass
"""

/*

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 submission:
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

Buggy python submission (verdict: Time Limit Exceeded):
while True:
	try:
		line = input().strip()
		if len(line)==0:break
		print(0)
	except EOFError:
		 pass
"""

/*

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 submission:
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

Buggy python submission (verdict: Runtime Error):
while True:
	line = input().strip()
	if len(line)==0:break
	print(0)

"""

/*

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 submission:
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

Buggy python submission (verdict: Runtime Error):
while 1:
    try:
        print ans[raw_input().replace(" ","")]
    except:
        break

"""

/*

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 submission:
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

Buggy python submission (verdict: Runtime Error):
try:
    while True:
        v=raw_input().split()
        print(m[''.join(v)][1])
except EOFError:
    pass
"""

/*

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 submission:
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

Buggy python submission (verdict: Runtime Error):
try:
    while True:
        v=raw_input().split()
        print(m[''.join(v)][1])
except EOFError:
    pass
"""

/*

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 submission:
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

Buggy python submission (verdict: Runtime Error):
m={}
try:
    while True:
        v=raw_input().split()
        print(m[''.join(v)][1])
except EOFError:
    pass

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
from __future__ import division
import sys
if sys.version_info[0]>=3: raw_input=input

X=4
Y=2
i=0
m={}

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

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
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:
	break

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

Fixed submission:
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

Buggy python submission (verdict: Runtime Error):
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:
	break

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

Fixed submission:
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