object Main {
    def main(args: Array[String]) = {
        val in = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt))
        val n = in.next.head
        val tree = in.map(r => (r.head-1, r.drop(2).map(_-1))).toList
        val res = List.fill(n)(Array.fill(3)(0))
        solve(1, tree(0))
        def solve(d: Int, t: (Int, Array[Int])): Int = {
            if (res(t._1)(2) == 1) {
                d-1
            } else {
                res(t._1)(0) = d
                val f_chld =
                    if (t._2.nonEmpty) {
                        t._2.foldLeft(d)((dd, id) => solve(dd+1, tree(id)))
                    } else {
                        d
                    }
                res(t._1)(1) = f_chld+1
                res(t._1)(2) = 1
                f_chld+1
            }
        }
        res.zipWithIndex.foreach{case (x, i) => println(i + 1 + " " + x.init.mkString(" "))}
    }
}