import scala.io.StdIn.{readInt,readLine}
  
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

    var list = Array(1)
    var cnt = 0
    while(list.length > 0 && cnt <= n) {
      list = (for(e <- list) yield {
        if(d(e-1) == -1) { d(e-1) = cnt }
        g(e-1)
      }).toArray.flatten
      cnt += 1
    }

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