import scala.collection.immutable.Queue;
import scala.io.StdIn.{readInt, readLine};
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val ig = new IntGraph(n);
        (1 to n).foreach { src => readLine.split(" ").drop(2).map(_.toInt).foreach { dst => ig.add(src, dst)}}
        val ret = ig.bSearch();
        (1 to n).foreach { i => println("%d %d".format(i, ret(i)))}
    }
}

class IntGraph(n: Int) {
    val size = n + 1;
    val matrix = Array.ofDim[Int](size, size);
    def add(src: Int, dst: Int): Unit = matrix(src)(dst) = 1;
    def bSearch(): Array[Int] = {
        val status = new Array[Int](size).map( e => -1);
        def _bSearch(que: Queue[Int]): Queue[Int] = {
            que.dequeueOption match {
                case None => que;
                case Some((src, restQue)) => {
                    val newQue = (1 until size).foldLeft(restQue) { (tmpQue, dst) =>
                        if (matrix(src)(dst) == 1 && status(dst) == -1) {
                            status(dst) = status(src) + 1
                            tmpQue.enqueue(dst);
                        } else {
                            tmpQue;
                        } 
                    }
                    _bSearch(newQue);
                }
            }
        }
        status(1) = 0;
        _bSearch(Queue[Int](1));
        status;
    }
}
