N = gets.chomp.to_i
ar = Array.new(N+1) { Array.new }
1.upto(N) do |i|
  arr = gets.chomp.split.map(&:to_i)
  arr.shift
  arr.shift
  ar[i] = arr
end

ans = Array.new(N+1, -1)
que = [[1,0]]
while !que.empty?
  n,c = que.shift
  next if ans[n] != -1 && ans[n] <= c
  ans[n] = c
  ar[n].each do |a|
    if ans[a] == -1 || ans[a] > c+1
      que.push([a,c+1])
    end
  end
end

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

