Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type vertexId int

type vertex struct {
	id vertexId
	firstVisitAt int
	finishVisitAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		firstVisitAt: 0,
		finishVisitAt: 0,
		adjs: adjs,
	}
}

func (v *vertex) doesAllAdjsVisited(vertices []vertex) bool {
	for _, vId := range v.adjs {
		adj := vertices[vId-1]
		if !adj.isVisited() {
			return false
		}
	}
	return true
}

func (v *vertex) isFirstVisit() bool {
	return v.firstVisitAt == 0
}

func (v *vertex) isVisited() bool {
	return v.firstVisitAt > 0
}

func (v *vertex) nextVertex(vertices []vertex) (vertexId, error) {
	for _, vId := range v.adjs {
		adj := vertices[vId-1]
		if !adj.isVisited() {
			return vId, nil
		}
	}
	return vertexId(-1), fmt.Errorf("illegal")
}

func dfs(currentId vertexId, timestamp int, vertices []vertex) int {
	timestamp = timestamp + 1
	currentVertex := &vertices[currentId-1]
	if currentVertex.isFirstVisit() {
		currentVertex.firstVisitAt = timestamp
	}
	for !currentVertex.doesAllAdjsVisited(vertices) {
		nextVertexId, _ := currentVertex.nextVertex(vertices)
		timestamp = dfs(nextVertexId, timestamp, vertices)
	}
	timestamp = timestamp + 1
	currentVertex.finishVisitAt = timestamp
	return timestamp
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	dfs(vertexId(1), 0, vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", vertices[i].id, vertices[i].firstVisitAt, vertices[i].finishVisitAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

Python: # -*- coding: utf-8 -*-

def DFS_stack(adj, start):
    n = len(adj)
    d = [0] * n
    f = [0] * n
    flag = [0] * n # 0:????¨????, 1:?¨??????????, 2:?¨??????????
    S = []
    time = 1

    S.append(start)
    flag[start] = 1
    d[start] = time
    time = time + 1
    
    while flag.count(2) != n: # ??¨????????????????????¢?´¢????????????????????§
        if len(S) != 0:
            u = S.pop()
            v = [i for i, x in enumerate(adj[u]) if (x == 1) and (flag[i] == 0)] 
            # v := ??£??\???????????????????????¢?´¢??????????????????
            if len(v) != 0:
                S.append(u)
                S.append(v[0])
                flag[v[0]] = 1
                d[v[0]] = time
                time = time + 1
            else:
            #??£??\??????????????¨????¨??????????
                flag[u] = 2
                f[u] = time
                time = time + 1

        else:
        	#??????????????????????????£?????´????????¢?´¢??????????????????????????¢?´¢????§?
            u = flag.index(0)
            S.append(u)
            flag[u] = 1
            d[u] = time
            time = time + 1            

    return d, f    

def DFS(adj, start):
    
    n = len(adj)
    d = [0] * n
    f = [0] * n
    flag = [0] * n
    time = 1
    
    def DFS_recursive(u, time):
        #print(u, flag)
        flag[u] = 1
        d[u] = time
        time = time + 1
        v = [i for i, x in enumerate(adj[u]) if x == 1]
        for i in v:
            if flag[i] == 0:
                time = DFS_recursive(i, time)
        flag[u] = 2
        f[u] = time
        time = time + 1
        return time
        
    time = DFS_recursive(start, time)
    for i in range(n):
        if flag[i] == 0:
           time =  DFS_recursive(i, time)
    
    return d, f



def main():
    
    n = int(input())
    adj = [[0 for i in range(n)] for j in range(n)]

    for i in range(n):
        tmp = list(map(int, input().split()))
        u = tmp[0]
        u = u -1
        k = tmp[1]
        v = tmp[2:]
        for j in range(k):
            adj[u][v[j]-1] = 1
            
    #d, f = DFS_stack(adj, 0)
    d, f = DFS(adj, 0)
    for i in range(n):
        print(i+1, d[i], f[i])

if __name__ == "__main__":
    main()

Python: #!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

WHITE = 0
GRAY = 1
BLACK = 2


def dbg_record(records):
    for index, record in enumerate(records):
        print("???????????????: %d" % index)
        if isinstance(record, dict):
            print("??\??????: %d" % record.get("visit", -1))
            print("?????????: %d" % record.get("leave", -1))
        else:
            print("?????????")
        print("")


def dfs(matrix):
    def internal_dfs(node_id, current_time):
        if state[node_id] == WHITE:
            # ?¨??????????
            state[node_id] = GRAY
            stack.append(node_id)
            records[node_id] = {"visit": current_time}  # ?¨??????????????¨????

            # ?¬?????¨????????????¨?????§????????????????????¢???
            next_id = None
            for index in range(len(matrix)):
                if matrix[node_id][index] == 1 and state[index] == WHITE:
                    next_id = index
                    break

            if next_id is not None:
                internal_dfs(next_id, current_time + 1)
            elif 0 < len(stack):
                # ?¬?????¨????????????¨?????§?????????????????????????????????????????°?????????
                internal_dfs(stack.pop(), current_time + 1)
        elif state[node_id] == GRAY:
            # ????????????

            # ?¬?????¨????????????¨?????§????????????????????¢???
            next_id = None
            for index in range(len(matrix)):
                if matrix[node_id][index] == 1 and state[index] == WHITE:
                    next_id = index
                    break

            if next_id is not None:
                stack.append(node_id)
                # internal_dfs(next_id, current_time + 1)
                internal_dfs(next_id, current_time)
            elif 0 < len(stack):
                # ?¬?????¨????????????¨?????§?????????????????????????????????????????°?????????
                state[node_id] = BLACK
                records[node_id]["leave"] = current_time  # ????????????????¨????
                internal_dfs(stack.pop(), current_time + 1)
            else:  # ??????????§????????????£???????????¨??? (node_id should be 0)
                state[node_id] = BLACK
                records[node_id]["leave"] = current_time
        elif state[node_id] == BLACK:
            print("Black!!")

    state = [WHITE] * len(matrix)  # White, Gray, Black
    records = [None] * len(matrix)  # Save time when the node is processed
    stack = []

    internal_dfs(0, 1)
    # dbg_record(records)
    return records


if __name__ == "__main__":
    # ??????????????????
    lines = [
        "6",
        "1 2 2 3",
        "2 2 3 4",
        "3 1 5",
        "4 1 6",
        "5 1 6",
        "6 0",
    ]
    # lines = sys.stdin.readlines()

    # Create n * n matrix
    dimension = int(lines[0])
    matrix = []
    for x in range(dimension):
        matrix.append([0] * dimension)

    # Set edge info
    for index, line in enumerate(lines[1:]):
        for edge in [int(x) for x in line.strip().split(" ")[2:]]:
            matrix[index][edge - 1] = 1

    result = dfs(matrix)
    for index, line in enumerate(result):
        print("%d %d %d" % (index + 1, line.get("visit", -1), line.get("leave", -1)))