object Main extends App {
  val n: Int = io.StdIn.readInt()
  val d: Array[Int] = Array.ofDim(n)
  val f: Array[Int] = Array.ofDim(n)
  val adjacencyMatrix: Seq[Array[Int]] =
    for (_ <- (0 until n).toVector)
      yield io.StdIn.readLine().split(" ").drop(2).map(_.toInt - 1)
  loop(0, 1)
  for (i <- 0 until n) println(s"${i+1} ${d(i)} ${f(i)}")
  
  def loop(current: Int, time: Int): Int = {
    var t: Int = time
    d(current) = time
    for(x <- adjacencyMatrix(current)) {
      if (d(x) == 0) t = loop(x, t+1)
    }
    t += 1
    f(current) = t
    t
  }
}
