table = {}

queue = [[[*'0'..'7'], 0]]
until queue.empty?
  current, num = queue.shift

  next if table[current.hash]
  table[current.hash] = num

  zero_pos = current.index('0')

  [zero_pos - 1, zero_pos + 1, zero_pos - 4, zero_pos, + 4].each do |pos|
    next unless pos.between?(0, 7)

    current2 = current.dup
    current2[zero_pos], current2[pos] = current2[pos], current2[zero_pos]
    queue << [current2, num + 1]
  end
end

while line = gets
  puts table[line.split(' ').hash]
end