Buggy python submission (verdict: Runtime Error):
import sys
memo = {}
sys.setrecursionlimit(40320)
def swap(state, a, b):
    state[a], state[b] = state[b], state[a]
def dfs(state, pos, c):
    key = "".join(map(str, state))
    if memo.has_key(key):
        return memo[key]
    memo[key] = c
    if pos!=0 and pos!=4:
        swap(state, pos-1, pos)
        dfs(state, pos-1, c+1)
        swap(state, pos-1, pos)
    if pos!=3 and pos!=7:
        swap(state, pos+1, pos)
        dfs(state, pos+1, c+1)
        swap(state, pos+1, pos)
    if pos<4:
        swap(state, pos+4, pos)
        dfs(state, pos+4, c+1)
        swap(state, pos+4, pos)
    else:
        swap(state, pos-4, pos)
        dfs(state, pos-4, c+1)
        swap(state, pos-4, pos)
    return c

dfs(range(8), 0, 0)
while True:
    try:
        state = raw_input().replace(" ","")
        print memo[state]
    except IOError:
        break

Fixed submission:
import Queue
memo = {}
def swap(state, a, b):
    state2 = state[:]
    state2[a], state2[b] = state2[b], state2[a]
    return state2
gkey = lambda state: "".join(map(str, state))

que = Queue.Queue()

init = range(8)
que.put((init, 0, 0))
memo[gkey(init)] = 0
while not que.empty():
    state, pos, c = que.get()
    def next_state(nxt):
        state2 = swap(state, nxt, pos)
        key = gkey(state2)
        if not memo.has_key(key):
            memo[key] = c+1
            que.put((state2, nxt, c+1))
    if pos!=0 and pos!=4:
        next_state(pos-1)
    if pos!=3 and pos!=7:
        next_state(pos+1)
    if pos<4:
        next_state(pos+4)
    else:
        next_state(pos-4)
while True:
    try:
        print memo[raw_input().replace(" ", "")]
    except EOFError:
        break

Buggy python submission (verdict: Runtime Error):
import sys
memo = {}
sys.setrecursionlimit(40320)
def swap(state, a, b):
    state[a], state[b] = state[b], state[a]
def dfs(state, pos, c):
    key = "".join(map(str, state))
    if memo.has_key(key):
        return memo[key]
    memo[key] = c
    if pos!=0 and pos!=4:
        swap(state, pos-1, pos)
        dfs(state, pos-1, c+1)
        swap(state, pos-1, pos)
    if pos!=3 and pos!=7:
        swap(state, pos+1, pos)
        dfs(state, pos+1, c+1)
        swap(state, pos+1, pos)
    if pos<4:
        swap(state, pos+4, pos)
        dfs(state, pos+4, c+1)
        swap(state, pos+4, pos)
    else:
        swap(state, pos-4, pos)
        dfs(state, pos-4, c+1)
        swap(state, pos-4, pos)
    return c

dfs(range(8), 0, 0)
while True:
    try:
        state = raw_input().replace(" ","")
        print memo[state]
    except KeyError:
        pass
    except IOError:
        break

Fixed submission:
import Queue
memo = {}
def swap(state, a, b):
    state2 = state[:]
    state2[a], state2[b] = state2[b], state2[a]
    return state2
gkey = lambda state: "".join(map(str, state))

que = Queue.Queue()

init = range(8)
que.put((init, 0, 0))
memo[gkey(init)] = 0
while not que.empty():
    state, pos, c = que.get()
    def next_state(nxt):
        state2 = swap(state, nxt, pos)
        key = gkey(state2)
        if not memo.has_key(key):
            memo[key] = c+1
            que.put((state2, nxt, c+1))
    if pos!=0 and pos!=4:
        next_state(pos-1)
    if pos!=3 and pos!=7:
        next_state(pos+1)
    if pos<4:
        next_state(pos+4)
    else:
        next_state(pos-4)
while True:
    try:
        print memo[raw_input().replace(" ", "")]
    except EOFError:
        break