import scala.io.StdIn.{readInt,readLine}
import scala.collection.mutable.ArrayBuffer

object Main {
  def main(args:Array[String]) = {
    val n = readInt
    val d = (1 to n).map(_ => -1).toArray

    val g = {
      for(i<-0 until n) yield {
        val v = readLine.split(" ").map(_.toInt)
        (for(j<-2 until 2+v(1)) yield v(j) ).toArray
      }
    }.toArray

    def scan(list:Array[Int], cnt:Int):Unit = {
      val a = new ArrayBuffer[Int]
      if(list.length>0 && cnt <= n) {
        for(e<-list if d(e-1) == -1) {
          d(e-1) = cnt
          g(e-1).foreach( a += _ )
        }
        scan(a.toArray,cnt+1)
      }
    }
    scan(Array(1),0)

    for(i<-0 until n) println((i+1) + " " + d(i))
  }
}