n = gets.to_i
line = []
(1..n).each{ |i| line[i] = gets.split.map(&:to_i)[2..-1] }
h = 0
visited = Array.new(n+1, -1)
visited[1] = 0
while true
  f = false
  (1..n).each do |i|
    if visited[i] == h
      f = true
      line[i].each{ |j| visited[j] = h+1 if visited[j] == -1 }
    end
  end
  break unless f
  h += 1  
end
(1..n).each{ |i| puts "#{i} #{visited[i]}" }