class Node
  attr_accessor :id, :nexts, :distance

  def initialize id
    @id = id.to_i
    @nexts = []
    @distance = -1
  end

  def setNexts next_id
    @nexts << next_id
  end
end

$input_map = {}
n = 0
count = 0
STDIN.each_line { |line|
  if count == 0
    count += 1
    n = line.to_i
    next
  end

  char_count = 0
  array = line.split " "
  id = array[0].to_i
  $input_map[id] = Node.new id
  array.each do |num|
    char_count += 1
    if char_count==1 || char_count==2
      next
    end
    $input_map[id].setNexts (Node.new num.to_i)
  end
}


$print_list = Array.new n + 1
def PrintNode id
  current_distance = $input_map[id].distance
  $print_list[id] = id, " ", current_distance, "\n"

  if $input_map[id].nexts.empty?
    return;
  end

  $input_map[id].nexts.each do |child|
    if child.distance == -1
      next_id = child.id
      $input_map[next_id].distance = current_distance + 1
      PrintNode child.id
    end
  end
end

$input_map[1].distance = 0
PrintNode 1
n.times do |i|
    print $print_list[i+1].join ""
end
# p $print_list

