N = gets.to_i
adjacency_list = []
N.times{
  u, _, *v = gets.split.map &:to_i
  adjacency_list[u] = v
}
d = Array.new(N+1, -1)
d[1] = 0
q = [1]
distance = 1
until q.empty?
  v = q.shift
  adjacency_list[v].each{|e_v|
    if d[e_v] == -1
      d[e_v] = distance
      q << e_v
    end
  }
  distance += 1
end

(1..N).each{|i|
  puts "#{i} #{d[i]}"
}