Go: package main

/*
深さ優先探索
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		ints := eGetInts(scanner)
		num, vcount := ints[0], ints[1]
		g[i] = make([]int, vcount)
		for j := 0; j < vcount; j++ {
			g[num][j] = ints[j+2]
		}
	}
	hist := make([]struct{ d, f int }, n+1)
	t := 0
	var visit func(int)
	visit = func(num int) {
		if hist[num].d == 0 {
			stack := []int{}
			t++
			hist[num].d = t
			stack = append(stack, num)
			for len(stack) > 0 {
				// トップを訪問する
				top := stack[len(stack)-1]
				for i := 0; i < len(g[top]); i++ {
					if hist[g[top][i]].d == 0 {
						t++
						hist[g[top][i]].d = t
						stack = append(stack, g[top][i])
						goto endloop
					}
				}
				// 訪問場所がなくなったら
				// pop
				stack = stack[0 : len(stack)-1]
				t++
				hist[top].f = t
			endloop:
			}
		}
	}
	for i := 1; i < n+1; i++ {
		visit(i)
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, hist[i].d, hist[i].f)
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

Python: n = int(input())

points = []
seen = []
find_time = []
end_time = []
for _ in range(n):
    input_text = list(map(int, input().split()))
    u, k, v_list = input_text[0], input_text[1], input_text[2:]
    seen.append(False)
    points.append(v_list)
    find_time.append(0)
    end_time.append(0)

count = [0]


def dfs(graph, v, count):
    seen[v] = True
    find_time[v] = count[0]
    for next_v in graph[v]:
        if seen[next_v - 1]:
            continue
        count[0] += 1
        dfs(graph, next_v - 1, count)
    count[0] += 1
    end_time[v] = count[0]


for index, seen_point in enumerate(seen):
    if seen_point:
        pass
    else:
        count[0] += 1
        dfs(points, index, count)
# print(find_time)
# print(end_time)


count = 1
for f, e in zip(find_time, end_time):
    print(count, f, e)
    count += 1

Python: # -*- coding: utf-8 -*-
"""
Created on Sun Jul  8 10:56:33 2018

@author: maezawa
"""
n = int(input())
adj = [[] for _ in range(n)]
d = [0 for _ in range(n)]
f = [0 for _ in range(n)]

for j in range(n):
    ain = list(map(int, input().split()))
    u = ain[0]
    k = ain[1]
    for i in range(k):
        adj[u-1].append(ain[i+2]-1)
    adj[u-1].sort()
    #print(*adj[u-1])

t = 1
stack = [0]

while stack:
    current = stack[-1]
    if d[current] == 0:
        d[current] = t
        t += 1
    flag = 0
    for k in adj[current]:
        if f[k] == 0:
            stack.append(k)
            flag = 1
            break
    if flag == 0:
        f[current] = t
        t += 1
        stack.pop()

    #print('current = {}, t = {}'.format(current, t))
    #print('stack', *stack)

for i in range(n):
    print('{} {} {}'.format(i+1, d[i], f[i]))

Python: def MAIN():
    n = int(input())
    ans = [[i, 0, 0] for i in range((n + 1))]
    G = [[] for _ in range((n + 1))]
    for _ in range(n):
        u, k, *v = map(int, input().split())
        G[u] = v
    cnt = 1
    def dfs(u):
        nonlocal ans
        if ans[u][1] != 0:
            return
        nonlocal cnt
        ans[u][1] = cnt
        cnt += 1
        for v in G[u]:
            dfs(v)
        ans[u][2] = cnt
        cnt += 1
    dfs(1)
    for i in range(1, n + 1):
        print(" ".join(map(str, ans[i])))
MAIN()

Python: def inpl():
    return list(map(int, input().split()))


def dfs(v, step):
    if f[v] != -1:
        return step
    f[v] = step
    step += 1
    for nv in E[v]:
        # print("now", v, nv, step)
        step = dfs(nv - 1, step)
    d[v] = step
    return step + 1


n = int(input())
E = [[] for i in range(n)]
for _ in range(n):
    tmp = inpl()
    u, k = tmp[:2]
    E[u - 1] = tmp[2:]

d = [-1 for i in range(n)]
f = [-1 for i in range(n)]

step = 1
for v in range(n):
    step = dfs(v, step)

for i in range(n):
    print(i + 1, f[i], d[i])

Python: n = int(input())
G = [[] for _ in range(n)]
u1 = 0
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
    if k != 0 and u1 == 0:
        u1 = u

time = 1
u = u1
D = []
F = []
out = [[i+1, 0, 0] for i in range(n)]
T = []
while True:
    if u not in D:
        out[u - 1][1] = time
        D.append(u)
        T.append(u)

    for f in F:
        if f in G[u - 1]:
            idx = G[u - 1].index(f)
            del G[u - 1][idx]

    if G[u - 1] == []:
        time += 1
        out[T.pop() - 1][2] = time
        F.append(u)
        if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
        else:
            u = T[-1]
    else:
        u = G[u - 1].pop(0)
        time += 1

for ni in range(n):
    print(*out[ni])

Python: count = 0


def solve(i, a, ans):
    global count
    if ans[i][0] == 0:
        count += 1
        ans[i][0] = count

    for x in range(a[i][1]):
        if ans[a[i][2 + x] - 1][0] == 0:
            solve(a[i][2 + x] - 1, a, ans)

    if ans[i][1] == 0:
        count += 1
        ans[i][1] = count


N = int(input())
a = []
for _ in range(N):
    a.append([int(x) for x in input().split()])

ans = [[0 for i in range(2)] for j in range(N)]

for i in range(N):
    if ans[i][0] == 0:
        solve(i, a, ans)

for i, x in enumerate(ans):
    print(i + 1, *x)