def bfs(puzzle)
  memo = {puzzle => 0}
  q = [puzzle]

  mv = [-4, 4, 1, -1]
  while p = q.shift
    return memo[p] if p == '01234567'

    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
end

while s = gets
  puts bfs(s.chomp.delete(' '))
end