data class Node(val id: Int, val edges: List<Int>, var discoveredTime: Int = 0, var finishedTime: Int = 0, var visited: Boolean = false)

fun main(args: Array<String>) {
    val nodeList = List(readLine()?.toInt() ?: 0) {
        val input = readLine()?.split(" ".toRegex())?.map(String::toInt) ?: listOf()
        Node(input[0], input.subList(2, 2 + input[1]))
    }
    val dfs = Dfs(nodeList)
    dfs.print()
}
class Dfs(private val nodes: List<Node>) {
    init {
        dfs()
    }

    private var time = 0

    fun print() {
        println(
            nodes.joinToString("\n") { node ->
                "${node.id} ${node.discoveredTime} ${node.finishedTime}"
            }
        )
    }

    private fun dfs() {
        nodes.forEach { edgeNode ->
                if (!edgeNode.visited) {
                    visit(edgeNode)
                }
            }
    }

    private fun visit(node: Node) {
        node.visited = true
        node.discoveredTime = ++time
        nodes.filter { node.edges.contains(it.id) }
            .forEach { edgeNode ->
                if (!edgeNode.visited) {
                    visit(edgeNode)
                }
            }
        node.finishedTime = ++time
    }
}
