import scala.io.StdIn.{readInt, readLine};
import scala.reflect.ClassTag;
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val g = new Graph(n);
        (0 until n).foreach { i => 
            readLine.split(" ").map(_.toInt).toList match {
                case src :: d :: dst => g.add(src, dst);
                case _ => ()
            }
        }
        g.breadthFirstSearch.foreach { e => println("%d %d".format(e._2, e._1))}
    }
}
class Graph(n: Int) {
    val size = n + 1;
    val matrix = Array.ofDim[Int](size, size);
    
    def add(src: Int, dsts: Seq[Int]): Unit = if (src > 0 && src < size) {
        dsts.foreach { d => if (d > 0 && d < size) matrix(src)(d) = 1};
    }
    
    def dump(): Unit = matrix.slice(1, size).foreach { a => println(a.slice(1, size).mkString(" "))}
    
    def breadthFirstSearch: Array[(Int, Int)] = {
        val distance = new Array[Int](size).map(a => -1);
        val que = new Queue[Int](size);
        def go(): Unit = que.deque() match {
            case None => ();
            case Some(target) => {
                (1 until size).foreach { i =>
                    if (matrix(target)(i) == 1 && distance(i) == -1) {
                        que.enque(i);
                        distance(i) = distance(target) + 1;
                    }
                }
                go();
            }
        }
        que.enque(1);
        distance(1) = 0;
        go();
        distance.zipWithIndex.slice(1, size);
    }
}
class Queue[A](size: Int)(implicit m: ClassTag[A]) {
    var inPtr = 0;
    var outPtr = 0;
    var cnt = 0;
    val q = m.newArray(size);
    
    def isEmpty: Boolean = cnt == 0;
    
    def isFull: Boolean = cnt == size;
    
    def enque(elem: A): Boolean = if (isFull) false else {
        q(inPtr) = elem;
        inPtr = if (inPtr == (size - 1)) 0 else inPtr + 1;
        cnt += 1;
        true;
    }
    
    def deque(): Option[A] = if (isEmpty) None else {
        val elem = q(outPtr);
        outPtr = if (outPtr == size - 1) 0 else outPtr + 1;
        cnt -= 1;
        Some(elem);
    }
}
