import scala.io.Source.stdin
import scala.collection.mutable.{ Queue }

case class Node(
  id: Int,
  children: List[Int],
  var distance: Int = -1
)

object Main {
  var currentDist: Int = 0
  var nodes: Array[Node] = null

  def main(argv: Array[String]) = {
    val head :: tail = stdin.getLines.toList
    val count = head.toInt
    nodes = new Array[Node](count)

    tail foreach { line =>
      val id :: length :: children = line.split(" ").map(_.toInt).toList
      nodes(id-1) = Node(id, children)
    }

    nodes.head.distance = 0
    val q = new Queue[Node]()
    q.enqueue(nodes.head)
    bfs(q)

    nodes foreach { node =>
      println(s"${node.id} ${node.distance}")
    }
  }

  def bfs(q: Queue[Node]): Unit = {
    while (q.length > 0) {
      val current = q.dequeue()
      current.children.foreach { n =>
        if (nodes(n-1).distance == -1) {
          nodes(n-1).distance = current.distance + 1
          q.enqueue(nodes(n-1))
        }
      }
    }
  }
}

