Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}
	}

	d := make([]int, n+1)
	f := make([]int, n+1)
	t := 1
	for i := 1; i <= n; i++ {
		t = dfs(&g, &d, &f, i, t)
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d %d\n", i, d[i], f[i])
	}
}

func dfs(g *[][]int, d *[]int, f *[]int, id int, t int) int {
	if (*d)[id] > 0 {
		return t
	}
	(*d)[id] = t
	t += 1
	for i := 0; i < len((*g)[id]); i++ {
		t = dfs(g, d, f, (*g)[id][i], t)
	}
	(*f)[id] = t
	return t+1
}

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


if __name__ == '__main__':

    n = int(input())
    M = [[0 for i in range(n)] for j in range(n)]
    for _ in range(n):
        l = list(map(int, input().split(" ")))
        u = l[0]
        k = l[1]
        if k != 0:
            for v in l[2:]:
                M[u-1][v-1] = 1
    
    color = [0]*n
    d = [0]*n
    f = [0]*n
    time = 0
    
    def dfs(u): #depth-first search
        global time
        color[u] = 1
        time += 1
        d[u] = time
        for v in range(n):
            if M[u][v] == 1 and color[v] == 0:
                dfs(v)
        color[u] = 2
        time += 1
        f[u] = time
    
    for u in range(n):
        if color[u] == 0:
            dfs(u)

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

Python: from collections import deque
n = int(input())
u = []
k = []
v = []

for i in range(n):
    tmp = list(map(int, input().split()))
    u.append(tmp[0])
    k.append(tmp[1])
    if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
        
check = [0]*n
intime = [0]*n
outtime = [0]*n
time = 1

def dfs(v, start):
    d = deque()
    d.append(start)
    global check
    global intime
    global outtime
    global time
    check[start-1] = 1
    intime[start-1] = time
    time += 1
    while len(d) > 0:
        visit = d[-1]
        tf = True
        for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
        if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1

dfs(v, 1)
for i in range(n):
    if check[i] == 0:
        dfs(v, i+1)
for i in range(n):
    print(i+1, intime[i], outtime[i])

Python: #import pysnooper
#import os,re,sys,operator,math,heapq,string
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy
setrecursionlimit(10**6)

#@pysnooper.snoop()
def dfs(v,s):
    seen[v]=True
    first[v]=s
    s+=1
    #print(v,g[v])
    for i in g[v]:
        if seen[i]:
            continue
        s=dfs(i,s)
    #print(s)
    last[v]=s
    return s+1

n=int(stdin.readline().rstrip())
seen=[False]*n
first=[0]*n
last=[0]*n
g=[[] for _ in range(n)]
s=1
for i in range(n):
    v=list(map(int,stdin.readline().rstrip().split()))
    for j in v[2:]:
        g[v[0]-1].append(j-1)
for i in range(n):
    if not seen[i]:
        s=dfs(i,s)
for i in range(n):
    print(i+1,first[i],last[i])

Python: n = int(input())

m = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    com = list(map(int, input().split()))
    for j in range(com[1]):
        m[i][com[j+2]-1] = 1

color = ["white" for i in range(n)]

d = [0 for i in range(n)]
f = [0 for i in range(n)]
time = 0

def dfs(u):
    global time
    time += 1
    color[u] = "gray"
    d[u] = time
    for i in range(n):
        if m[u][i] and color[i] == "white":
            dfs(i)
    color[u] = "black"
    time += 1
    f[u] = time

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

Python: n = int(input())
mat = [[0] * n for _ in range(n)]
d, f = [0] * n, [0] * n
for _ in range(n):
    adj = list(map(int, input().split()))
    i = adj[0]
    v = adj[2:]
    for j in v:
        mat[i - 1][j - 1] = 1

time, current = 0, 0
stack = []
while 0 in f:
    if not stack:
        for i, v in enumerate(f):
            if v == 0:
                stack.append(i)
                time += 1
                d[i] = time
                break
    time += 1
    current = stack[-1]
    for i in range(len(mat[current])):
        if mat[current][i] and not d[i]:
            current = i
            d[i] = time
            stack.append(i)
            break
    else:
        f[current] = time
        current = stack.pop()

for l in zip(range(1, n + 1), d, f):
    print(*l)

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write

n = int(input())
G = [None for i in range(n+1)]
for i in range(n):
    node_id, k, *adj = map(int, readline().split())
    # adj.sort()
    G[node_id] = adj

begin = [0,]*(n+1)
end = [0,]*(n+1)
time = 1

def dfs(v):
    global time

    begin[v] = time

    adjlen = len(G[v])
    for i in range(adjlen):
        if begin[G[v][i]] != 0:
            continue
        time += 1
        dfs(G[v][i])
    time += 1
    end[v] = time

for i in range(1, n+1):
    if begin[i] == 0:
        if i != 1:
            time += 1 #新しく始点を選ぶのにもコストがかかる
        dfs(i)

for i in range(1, n+1):
    write(str(i) + " " + str(begin[i]) + " " + str(end[i]) + "\n")

Python: import collections,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n = I()
ukv = [LI() for _ in range(n)]
graph = {i:collections.deque() for i in range(1,n+1)} #1_indexed
for x in ukv:
    u,k,*v = x
    for y in v:
        graph[u].append(y)
seen = [0]*(n+1) #1_indexed
stack = []
time = 0
seentime = [0]*(n+1) #1_indexed
donetime = [0]*(n+1) #1_indexed
def dfs(j):
    global time
    seen[j] = 1
    stack.append(j)
    time += 1
    seentime[j] = time
    while stack:
        s = stack[-1]
        if not graph[s]:
            stack.pop()
            time += 1
            donetime[s] = time
            continue
        g_NO = graph[s].popleft()
        if seen[g_NO]:
            continue
        seen[g_NO] = 1
        stack.append(g_NO)
        time += 1
        seentime[g_NO] = time
for j in range(1,n+1):
    if seen[j]:
        continue
    dfs(j)
for k,a,b in zip(range(1,n+1),seentime[1:],donetime[1:]):
    print(k,a,b)

Python: def dfs(v, u=0):
    global n, t, d, f, reached
    if len(reached) == n:
        return

    d[u] = t
    t += 1

    reached.add(u + 1)
    for ui in range(v[u][1]):
        next = v[u][2 + ui]
        if next in reached:
            continue
        dfs(v, next - 1)

    f[u] = t
    t += 1


n = int(input())
v = []
for _ in range(n):
    v.append(list(map(int, input().split())))

t = 1
d = [0] * n
f = [0] * n
reached = set()


for ui in range(n):
    if ui + 1 not in reached:
        dfs(v, ui)

for ui in range(n):
    print('%d %d %d' % (ui + 1, d[ui], f[ui]))

Python: import sys

n = int(input())
G = [None]*(n+1)
for i in range(n):
    u, k, *vs = map(int, input().split())
    G[u] = vs

d = [-1]*(n+1)
f = [-1]*(n+1)
lb = iter(range(1, 2*n+1)).__next__
def dfs(v):
    d[v] = lb()
    for w in G[v]:
        if d[w] != -1: #already visited
            continue
        dfs(w)
    f[v] = lb()

for v in range(1, n+1):
    if d[v] != -1:
        continue
    dfs(v)

for v in range(1, n+1):
    print(f"{v} {d[v]} {f[v]}")