#
# ???????????¢?´¢
#
n = gets.chomp.to_i
graph = [[]]
n.times do
  graph << gets.chomp.split.map { |s| s.to_i }
end

dis = []
que = []

dis[1] = 0
que << 1

p graph

until que.empty?
  current_node = que.shift
  graph[current_node][1].times do |i|
    next_node = graph[current_node][2+i]
    if dis[next_node].nil? 
      dis[next_node] = dis[current_node] + 1
    end
    que << next_node
    p que
  end
end

1.upto(n) do |i|
  puts "#{i} #{dis[i]}"
end