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, -1)
$q = []
def calc_d v, distance
  $d[v] = distance if $d[v] == -1
  $adjacency_list[v].each{|e|
    $d[e] = distance + 1 if $d[e] == -1
  }
  $q += $adjacency_list[v] unless $adjacency_list.empty?
  until $q.empty?
    t_v = $q.shift
    calc_d t_v, distance + 1
  end
end
calc_d(1, 0)

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