object Main extends App {
    val in = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt))
    val n = in.next.head
    val tree = in.map(_.drop(2).map(_ - 1)).toList
    val res = List.fill(n)(Array.fill(2)(0))
    def solve(d: Int, id: Int, ids: Array[Int]): Int =
        if (res(id)(0) != 0) {
            d - 1
        } else {
            res(id)(0) = d
            val f_chld = ids.foldLeft(d)((_d, _id) => solve(_d + 1, _id, tree(_id)))
            res(id)(1) = f_chld + 1
            f_chld + 1
        }

    var x = 1
    for (i <- 0 until n if res(i)(0) == 0) {
        x = solve(x, i, tree(i)) + 1
    }
    res.zipWithIndex.foreach{case (x, i) => println(i + 1 + " " + x.mkString(" "))}
}