# Node
class Node
  attr_accessor :id, :edges, :visited, :cost

  def initialize(id, edges = [], visited = false, cost = nil)
    @id, @edges, @visited, @cost = id, edges, visited, cost
  end
end

# Edge
class Edge
  attr_accessor :to

  def initialize(to)
    @to = to
  end
end

# Graph
class Graph
  attr_accessor :nodes

  def initialize(data)
    @nodes = data.map do |id, edges|
      edges.map! { |edge| Edge.new(*edge) }
      Node.new(id, edges)
    end
  end

  def bfs(start)
    @nodes.each do |node|
      if node.id == start
        node.visited = true
        node.cost = 0
      else
        node.visited = false
        node.cost = -1
      end
    end

    queue = []
    queue << @nodes.find { |node| node.id == start }

    while queue.any?
      node = queue.shift
      node.visited = true
      node.edges.each do |edge|
        id = edge.to
        visited_node = @nodes.find { |item| item.id == id }
        unless visited_node.visited
          visited_node.cost = node.cost + 1 if visited_node.cost == -1
          queue << visited_node
        end
      end
    end
  end
end

n = gets.chop.to_i

data = {}

n.times do
  u, k, *v = gets.chop.split.map(&:to_i)
  data[u] = v
end

g = Graph.new data
g.bfs(1)

g.nodes.each do |node|
  puts "#{node.id} #{(node.visited) ? node.cost : -1}"
end