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 status = new Array[Int](size).map(a => -1);
        def go(q: Queue[Int]): Unit = {
            q.dequeueOption match {
                case None => ();
                case Some((src, rest)) => {
                    val newQ = graph(src).foldLeft(rest) { (nq, dst) =>
                        if (status(dst) == -1) {
                            status(dst) = status(src) + 1;
                            nq.enqueue(dst);
                        } else {
                            nq;
                        }
                    }
                    go(newQ);
                }
            }
        }
        status(start) = 0;
        go(Queue[Int](start));
        status;
    }
}
