s = '01234567'
memo = {s => 0}
q = [s]

mv = [-4, 4, 1, -1]
while p = q.shift
  i = p.index('0')
  mv.each do |m|
    j = i + m
    next if j < 0 || j > 7
    pn = p.dup
    pn[i], pn[j] = pn[j], pn[i]
    next if memo[pn]
    memo[pn] = memo[p] + 1
    q.push pn
  end
end

while s = gets
  puts memo[s.chomp.delete(' ')]
end