import scala.io.Source.stdin
import scala.collection.mutable.{ SortedSet => Set }

case class Node(
  id: Int,
  children: List[Int],
  start: Int = 1,
  end: Int = 1
)

object Main {
  var time: Int = 0
  var checked = Set[Int]()

  def main(argv: Array[String]) = {
    val head :: tail = stdin.getLines.toList
    val count = head.toInt
    val nodes = new Array[Node](count)

    tail foreach { line =>
      val id :: length :: children = line.split(" ").map(_.toInt).toList
      nodes(id-1) = Node(id, children)
    }

    nodes.foreach { node =>
      if(!checked.contains(node.id)) {
        checked += node.id
        time = time + 1
        recursive(nodes.head, nodes)
        time = time + 1
        nodes(0) = nodes(0).copy(end = time)
      }
    }

    nodes foreach { node =>
      println(s"${node.id} ${node.start} ${node.end}")
    }
  }

  def recursive(node: Node, nodes: Array[Node]): Unit = {
    node.children foreach { id =>
      if (!checked.contains(id)) {
        time = time + 1
        nodes(id-1) = nodes(id-1).copy(start = time)
        checked += id
        recursive(nodes(id-1), nodes)
        time = time + 1
        nodes(id-1) = nodes(id-1).copy(end = time)
      }
    }
  }
}

