N = gets.to_i
$adjacency_list = []
N.times{
  u, k, *v = gets.split.map &:to_i
  $adjacency_list[u] = v
}
$d = Array.new(N, -1)
def calc_d v, distance
  $d[v] = distance
  $adjacency_list[v].each{|each_v|
    calc_d each_v, distance+1
  }
end
calc_d(1, 0)

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