def toPuzzle(str)
  a,b,c,d,e,f,g,h=str.split("")
  return [[a,b,c,d],[e,f,g,h]]
end

while gets
  init = $_.chomp.split.join
  hash = Hash.new
  hash[init]=0
  q = Array.new
  q << init
  goal = "01234567"
  loop do
    str = q.shift
    if str == goal || str.nil?
      break
    end
    puzzle = toPuzzle(str)
    tmp =  str.split("").index("0")
    x = tmp%4
    y = tmp<4 ? 0 : 1
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    0.upto(3) do |i|
      nx = x + dx[i]
      ny = y + dy[i]
      if nx>=0 && nx < 4 && ny>=0 && ny<2
        tmp = Marshal.load(Marshal.dump(puzzle))
        tmp[ny][nx],tmp[y][x] = tmp[y][x],tmp[ny][nx]
        nstr = tmp.flatten.join
        next if hash[nstr]
        hash[nstr]=hash[str]+1
        q<<nstr
      end
    end
  end
  puts hash[goal]
end