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: n = int(input())

A = [[] for i in range(n)]

for i in range(n):
  A[i] = list(map(int, input().split()))

B = [[0 for i in range(2)] for j in range(n)]
order = []

time = 0

def dfs(n, num):
  global time
  while time < 2*n:
    a = num
    time += 1
    if B[num-1][0] == 0: # まだ発見していないとき
      if A[num-1][1] != 0: # 次の頂点があるとき
        order.append(A[num-1][0]) # 発見したことを記録する
        B[num-1][0] = time # 発見時刻を記録する
        A[num-1][1] -= 1 # 次の頂点数を１減らす
        if B[A[num-1][2]-1][0] == 0: # 次の頂点がまだ発見されていないとき
          num = A[num-1].pop(2) # 次の頂点に移動
        else: # 次の頂点が発見済みのとき
          del A[num-1][2] # 次の頂点をルートから削除
      else: # 次の頂点がないとき
        l = len(order) # これまで経由した頂点数
        order.append(A[num-1][0]) # 発見したことを記録する
        order.append(A[num-1][0]) # 調べ終えたことを記録する
        B[num-1][0] = time # 発見時刻を記録する
        time += 1 # 時刻を１進める
        B[num-1][1] = time # 調査終了時刻を記録する
        num = order[l-1] # １つ前の頂点に戻る
    elif  B[num-1][1] == 0: # 発見しているが、調べ終えていないとき
      if A[num-1][1] == 0: # 次の頂点がないとき
        order.append(A[num-1][0]) # 調べ終えたことを記録する
        B[num-1][1] = time # 調査終了時刻を記録する
        m = order.index(num) # 発見した順番を調べる
        num = order[m-1] # 発見する前の頂点に移動
      else: # 次の頂点があるとき
        A[num-1][1] -= 1 # 次の頂点数を１減らす
        order.append(A[num-1][0]) # 経由したことを記録する
        if B[A[num-1][2]-1][0] == 0: # 次の頂点がまだ発見されていないとき
          num = A[num-1].pop(2) # 次の頂点に移動
        else: # 次の頂点が発見済みのとき
          del A[num-1][2] # つぎの頂点をルートから削除
        time -= 1 # 時刻を戻す
    else: # すでに調査終了した頂点に移動したとき
      l = len(order) # これまで経由した頂点数
      num = order[l-1] # １つ前の頂点に戻る
      time -= 1 # 時刻を戻す
      if num == a:
        break

for i in range(n):
  if B[i][0] == 0:
    dfs(n, A[i][0])

for i in range(n):
  print(str(i+1) + " ", end="")
  print(" ".join(map(str, B[i])))

Python: if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline

    N = 100
    WHITE = 0
    GRAY = 1
    BLACK = 2

    M = [[0]*N for _ in range(N)]
    color = [WHITE]*N  # colorの初期化は最初に行っておく。
    d = [0]*N
    f = [0]*N

    def dfs_visit(u):
        global tt
        color[u] = GRAY
        tt += 1
        d[u] = tt # 最初の訪問
        for v in range(n):
            if M[u][v] == 0:
                continue
            if color[v] == WHITE:
                dfs_visit(v)
        color[u] = BLACK
        tt += 1
        f[u] = tt # 訪問の完了

    def dfs():
        global tt
        tt = 0

        for u in range(n):
            if color[u] == WHITE:
                # 未訪問のuを始点として深さ優先探索
                dfs_visit(u)
        for u in range(n):
            print(str(u+1) + ' ' + str(d[u]) + ' ' + str(f[u]))

    n = int(input())
    for _ in range(n):
        u, k, *vv = input().split()
        u = int(u)-1  # 0オリジンにする
        k = int(k)    # kは使わない
        for v in vv:
            v = int(v)-1
            M[u][v] = 1
    dfs()

Python: def depth(s, time):
    time += 1
    flag[s] = 1
    d[s] = time
    for i in range(1, n+1):
        if G[s][i] == 1:
            if flag[i] == 0:
                depth(i, time)
                time = f[i]
    f[s] = time + 1
 
n = int(raw_input())
G = [[0 for i in range(n+1)] for j in range(n+1)]
d = [0 for i in range(n+1)]
f = [0 for i in range(n+1)]
 
for i in range(n):
    v = map(int, raw_input().split())
    for j in range(v[1]):
        G[v[0]][v[2+j]] = 1
 
flag = [0 for i in range(n+1)]
for i in range(1, n+1):
    if flag[i] == 0:
        depth(i, max(f))
 
for i in range(1, n+1):
    print(str(i) + " " + str(d[i]) + " " + str(f[i]))

Python: def visit(u):
    global visit_color, d, f, t
    color[u] = GRAY
    d[u] += t
    t += 1
    for v in range(n):
        if (M[u][v] == 0):
            continue
        else:
            if (color[v] == WHITE):
                visit(v)
    color[u] = BLACK
    f[u] = t
    t += 1

n = int(input())
M = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
    tmp = list(map(int, input().split()))
    for i in range(tmp[1]):
        M[tmp[0]-1][tmp[i+2]-1] = 1

for u in range(n):
    if (color[u] == WHITE):
        visit(u)
for u in range(n):
    print(str(u+1), str(d[u]), str(f[u]), sep = " ")

Python: from collections import deque
q = deque()

n = int(input())
C = [deque() for _ in range(n+1)]

for i in range(n):
    tmp = list(map(int,input().split()))
    for j in range(tmp[1]):
        C[tmp[0]].append(tmp[2+j])

visited = [False]*(n+1)
d = [0]*(n+1)
f = [0]*(n+1)
t = 0

def stack(i):
    global t
    if visited[i] == False:
        q.append(i)
    while q:
        #ノードの探索開始
        node = q[-1]
        if d[node] == 0:
            t += 1
            d[node] = t
        visited[node] = True
        for _ in range(len(C[node])):
            tmp = C[node].popleft()
            if visited[tmp] == False:
                next_node = tmp
                q.append(next_node)    #未探索の接続ノード
                break
        
        #未探索接続ノードがなければそのノードは探索終了
        else:
            t += 1
            f[q.pop()] = t

for i in range(1,n+1):
    stack(i)

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

Python: from sys import stdin
n = int(stdin.readline())
M = [0] + [list(map(int, stdin.readline().split()[2:])) for i in range(n)]
sndf = [0] + [[False, i] for i in range(1, n + 1)]
tt = 0
def dfs(u):
    global tt
    sndf[u][0] = True
    tt += 1
    sndf[u].append(tt)
    for v in M[u]:
        if not sndf[v][0]:
            dfs(v)
    tt += 1
    sndf[u].append(tt)
for i in range(1, n + 1):
    if not sndf[i][0]:
        dfs(i)
    print(*sndf[i][1:])

Python: def curserch(N,k,t,P,n):
    g = 0
    t = t+1
    P[k]["d"] = t
    for i in range(1,n+1):
        if N[k][i]==1 and P[i]["d"]==0:
            P[i]["f"] = g = curserch(N,i,t,P,n)
    if g !=0:
        return g+1
    else:
        return t+1

n = int(input())
A = [[0 for j in range(n+1)] for i in range(n+1)]


for i in range(n):
    vec = input().split()
    u = int(vec[0])
    k = int(vec[1])
    nodes = vec[2:]
    for i in range(int(k)):
        v = int(nodes[i])
        A[u][v] = 1

P=[{"d":0} for i in range(n+1)]
P[1]["f"] = curserch(A,1,0,P,n)
for i in range(1,n+1):
    print(i,P[i]["d"],P[i]["f"])

Python: """Depth First Search."""

n = int(input())

adj = [None]
for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i)

# searched or not, number of vertex, discovery time and finishing time.
sndf = [None]
for i in range(1, n + 1):
    sndf.append([False, i])
sndf.append(0)

path = []

time = 0

def dfs(u):
    global time
    path.append(u)
    time += 1
    sndf[u].append(time)

    while path:
        u = path[-1]
        if adj[u]:
            v = adj[u].pop(0)
            if not sndf[v][0]:
                sndf[v][0] = True
                time += 1
                sndf[v].append(time)
                path.append(v)
        else:
            path.pop()
            sndf[u][0] = True
            time += 1
            sndf[u].append(time)


for i in range(1, n + 1):
    if not sndf[i][0]:
        dfs(i)

for x in sndf[1:-1]:
    print(*x[1:])