import scala.io.StdIn.{readInt, readLine};

object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val matrix = Array.ofDim[Int](n + 1, n + 1);
        (1 to n).foreach { i =>
            val line = readLine.split(" ");
            line.slice(2, line.length).map(_.toInt).foreach { j =>
                matrix(i)(j) = 1;
            }
        }
        val ret = bfs(matrix, 1);
        (1 to n).foreach { i => println("%d %d".format(i, ret(i)))}
    }
    
    def bfs(matrix: Array[Array[Int]], start: Int): Array[Int] = {
        val len = matrix.length;
        val status = new Array[Int](len).map( i => -1);
        def go(que: IntQueue): Unit = {
            que.peek match {
                case None => ();
                case Some(v) => {
                    val cost = status(v) + 1
                    val newQue = (1 until len).
                        filter(i => matrix(v)(i) == 1 && status(i) == -1).
                        foldLeft(que) { (q, i) => 
                            status(i) = cost;
                            q.enqueue(i);
                        }
                    go(newQue.dequeue);
                }
            }
        }
        status(start) = 0;
        go(IntQueue(start));
        status;
    }
}

class IntQueue(private val addList: List[Int], private val popList: List[Int]) {
    def peek(): Option[Int] = if (popList == Nil) None else Some(popList.head);
    def enqueue(e: Int): IntQueue = {
        if (popList == Nil) {
            new IntQueue(Nil, (e :: addList).reverse);
        } else {
            new IntQueue(e :: addList, popList);
        }
    }
    def dequeue(): IntQueue = {
        popList match {
            case Nil => throw new NoSuchElementException;
            case head :: Nil => new IntQueue(Nil, addList.reverse);
            case head :: tail => new IntQueue(addList, tail);
        }
    }
}
object IntQueue {
    def apply(): IntQueue = new IntQueue(Nil, Nil);
    def apply(elems: Int*): IntQueue = new IntQueue(Nil, elems.toList.reverse);
}
