n = gets.to_i
vlist = []
n.times do
  u, k, *v = gets.split.map {|i| i.to_i}
  vlist[u-1] = v
end

q = []
q.push 0
d = Array.new(n, nil)
d[0] = 0

while !q.empty?
  u = q.shift
  vlist[u].each do |i|
    unless d[i-1]
      d[i-1] = d[u] + 1
      q.push i-1
    end
  end
end

d = d.map{ |i| i ? i:-1 }
d.each_with_index do |i, idx|
  puts "#{idx+1} #{i}"
end
