import scala.io.StdIn.{readLine, readInt};
import scala.collection.immutable.Queue;
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val graph: Array[List[Int]] = new Array[List[Int]](n + 1);
        for (i <- (1 to n)) {
            graph(i) = readLine.split(" ").drop(2).map(_.toInt).toList;
        }
        val ret: Array[Int] = bfs(1, n + 1, graph);
        for (i <- (1 to n)) {println("%d %d".format(i, ret(i)))}
    }
    def bfs(start: Int, size: Int, graph: Array[List[Int]]): Array[Int] = {
        val ret = new Array[Int](size).map(a => -1);
        def go(q: Queue[Int]): Unit = {
            q match {
                case Queue() => ();
                case e +: rest => {
                    val cost = ret(e) + 1;
                    val newQ = graph(e).foldLeft(rest) { (nq, i) =>
                        if (ret(i) == -1) {ret(i) = cost; i +: nq} else {nq}
                    }
                    go(newQ);
                }
            }
        }
        ret(start) = 0;
        go(Queue(start));
        ret;
    }
}
