def find_all(start_node)
  # Node => min dist
  node_to_min_dist_hash = Hash.new(-1)

  open_list   = [[start_node, 0]]
  closed_list = []

  while open_list.size != 0
    node, dist = open_list.shift

    if closed_list.include?(node)
      next
    end

    if !node_to_min_dist_hash.has_key?(node)
      node_to_min_dist_hash[node] = dist
    end

    nodes = $adj_lists[node]
    nodes.each{|n|
      open_list.push([n, dist+1])
    }
    closed_list.push(node)
  end

  return node_to_min_dist_hash
end


# NOTE: 1-origin (not 0-origin)
$adj_lists = []

$cache= {}

_ = gets


while (line = gets)
  i, _, *nodes= line.split(' ').map(&:to_i)
  $adj_lists[i] = nodes
end

node_to_min_dist_hash = find_all(1)

# Print
(1...$adj_lists.size).each{|target_node|
  min_dist = node_to_min_dist_hash[target_node]
  puts("#{target_node} #{min_dist}")
}


