object Main extends App {
  val n = io.StdIn.readInt()
  val adjacents = Array.fill[Array[Int]](n)(Array())
  for (_ <- 0 until n) {
    val data = io.StdIn.readLine().split(" ").map(_.toInt)
    val id = data(0)-1
    adjacents(id) = data.drop(2).map(_-1)
  }
  val depth = Array.fill[Int](n)(-1)
  def search(x: Int) = {
    val queue = collection.mutable.Queue.empty[Int]
    depth(x) = 0
    queue.enqueue(x)
    loop()
    def loop(): Unit =
      if (queue.isEmpty) ()
      else {
        val i = queue.dequeue()
        for (a <- adjacents(i))
          if (depth(a) < 0) {
            depth(a) = depth(i)+1
            queue.enqueue(a)
          }
        loop()
      }
  }
  search(0)
  for (i <- 0 until n) println(s"${i+1} ${depth(i)}")
}
