def bfs(start, vertices, watcher)
  watcher[start] = 0
  store = [start]
  while !store.empty? do
    v = store.shift
    vertices[v].each do |i|
      if watcher[i] < 0 then
        watcher[i] = watcher[v] + 1
        store << i
      end
    end
  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)
watcher.each do |key, val|
  puts "#{key} #{val}"
end