def bfs(id, vertices, watcher, time)
  watcher[id] = time
  to_visit = []
  vertices[id].each do |neighbor|
    if (watcher[neighbor] < 0) then
      to_visit << neighbor
    end
  end
  time += 1
  to_visit.each do |neighbor|
    bfs(neighbor, vertices, watcher, time)
  end
end

# main
n = gets.chomp.to_i
vertices = {}
watcher = {}
(n).times do
  inputs = gets.chomp.split.map(&:to_i)
  neighbors = (inputs[1] > 0) ? inputs[2..-1] : []
  vertices[inputs[0]] = neighbors
  watcher[inputs[0]] = -1
end

bfs(1, vertices, watcher, 0)
watcher.each do |key, val|
  puts "#{key} #{val}"
end