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 { i => readLine.split(" ").drop(2).map(_.toInt).foreach { e => ig.add(i, e)}}
    val ret = ig.dSearch();
    (1 to n).foreach { i => println("%d %d %d".format(i, ret(i)(0), ret(i)(1)))}
  }
}
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 dSearch(): Array[Array[Int]] = {
    val status = Array.ofDim[Int](size, 2);
    def _dSearch(stack: List[Int], cost: Int): (List[Int], Int) = {
      stack match {
        case Nil => (stack, cost);
        case x :: rest => {
          status(x)(0) = cost;
            val (newStack, newCost) = (0 until size).foldLeft((rest, cost)) { (ret, i) =>
              if (matrix(x)(i) == 1 && status(i)(0) == 0) _dSearch(i :: ret._1, ret._2 + 1) else (ret._1, ret._2);
            }
          status(x)(1) = newCost + 1;
          (newStack, status(x)(1));
        }
      }
    }
    (1 until size).foldLeft(0) { (cost, i) =>
      if (status(i)(0) == 0) {
        _dSearch(List[Int](i), cost + 1)._2;
      } else {
        cost
      }
    }
    status;
  }
}
