Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 最初にiを訪問した時刻。発見時刻
//f[i]: iの隣接リストを訪問し終えた時刻。完了時刻
var color, d, f [MAX]int

func dfs_visit(u int) {
	color[u] = GRAY
	time++
	d[u] = time
	//隣接を調べる
	for v := 0; v < n; v++ {
		if M[u][v] == 0 {
			continue
		}
		//隣接するvが、未訪問だった場合
		if color[v] == WHITE {
			dfs_visit(v)
		}
	}
	//訪問完了処理
	color[u] = BLACK
	time++
	f[u] = time
	//訪問完了
}

func dfs() {
	//初期化
	for u := 0; u < n; u++ {
		color[u] = WHITE
	}
	time = 0

	//未訪問のuを始点として、深さ優先探索
	for u := 0; u < n; u++ {

		if color[u] == WHITE {
			dfs_visit(u)
		}
	}

	for u := 0; u < n; u++ {
		fmt.Println(u+1, d[u], f[u])
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	dfs()

}

Python: n = int(input())
g = [None] * n
visited = [0] * n
root = set(range(n))
ts = [[0, 0] for _ in range(n)]
t = 0

while n:
    l = list(map(int, input().split()))
    connected = [i - 1 for i in l[2:]]
    g[l[0] - 1] = [i - 1 for i in l[2:]]
    root -= set(connected)
    n -= 1


def dfs(i):
    global g, t, ts, visited
    lts = ts[i]
    t += 1
    lts[0] = t
    visited[i] = 1
    for c in g[i]:
        if not visited[c]:
            dfs(c)
    t += 1
    lts[1] = t


for r in sorted(root):
    dfs(r)

for i, v in enumerate(ts):
    print(i + 1, *v)

Python: t = 1
v, d, f = {}, {}, {}

def dfs(i):
  global t, v, d, f
  if i in d:
    return
  if i != 0:
    d[i] = t
    t += 1
  for j in v[i]:
    dfs(j)
  if i != 0:
    f[i] = t
    t += 1

n = int(input())
v[0] = list(range(1, n+1))
for i in range(n):
  tmp = list(map(int, input().split()))
  v[tmp[0]] = tmp[2:]
  v[tmp[0]].sort()
dfs(0)
for i in range(n):
  print("{} {} {}".format(i+1, d[i+1], f[i+1]))

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

v_color = ["WHITE"] * (n+1) # WHITE:????¨???????GRAY:?¨??????????BLACK:??¢?´¢??????
d = [0] * (n+1)
f = [0] * (n+1)
stack = []

def DFS(u):
    time = 0
    stack.append(u)
    
    while stack != []:
        u = stack[-1]
        time += 1
        
        v_color[u] = "GRAY"
        if d[u] == 0:
            d[u] = time
        
        for Next in graph[u-1][2:]:
            if v_color[Next] == "WHITE":
                stack.append(Next)
                break
        
        else:
            v_color[u] = "BLACK"
            f[u] = time +1
            stack.pop()
        
DFS(1)
    
for i in range(1,n+1):
    print(i,d[i],f[i])

Python: def dfs_recursive(u):
    global time
    color[u] = 'gray'
    arrive[u] = time
    time += 1
    for v in range(n):
        if adjMat[u][v] == 1 and color[v] == 'white':
            dfs_recursive(v)
    color[u] = 'black'
    finish[u] = time
    time += 1


n = int(input())

adjMat = [[0] * n for _ in range(n)]
color = ['white'] * n
arrive = [0] * n
finish = [0] * n
time = 1

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

for root in range(n):
    if color[root] == 'white':
        dfs_recursive(root)


for i in range(n):
    out = ''
    out += '{} {} {}'.format(i + 1, arrive[i], finish[i])
    print(out)

Python: def dfs(i, cnt):
    D[i] = cnt
    cnt += 1
    for c in v[i]:
        if D[c-1] == -1:
            cnt = dfs(c-1, cnt)
    F[i] = cnt
    cnt += 1
    return cnt


N = int(input())
g = [0]*N
k = [0]*N
v = [[] for _ in range(N)]
for i in range(N):
    tmp = list(map(int, input().split()))
    g[i] = tmp[0]
    k[i] = tmp[1]
    if k[i] != 0:
        v[i] = sorted(tmp[2:])

D = [-1] * N
F = [-1] * N
cnt = 1
for i in range(N):
    if D[i] == -1:
        cnt = dfs(i, cnt)

for i, (d, f) in enumerate(zip(D, F)):
    print(i+1, d, f)

Python: # vim: set fileencoding=utf-8:
import sys
from functools import lru_cache

n = int(sys.stdin.readline().rstrip())
graph = [[0] * (n+1) for _ in range(n+1)]
for i in range(1, n+1):
    tmp = [int(ele) for ele in sys.stdin.readline().rstrip().split()]
    for j in tmp[2:]:
        graph[i][j] = 1

d = [-1] * (n+1)
f = [-1] * (n+1)
yet = [ele for ele in range(1, n+1)]
ct = 0


def main():
    while yet:
        stack = [min(yet)]
        dfs(stack)

    for i, detected_at, finished_at in zip(range(1, n + 1), d, f):
        print(str(i) + " " + str(detected_at) + " " + str(finished_at))


def dfs(stack):
    global yet, ct, graph, d, f
    ct += 1
    d[stack[-1]-1] = ct
    yet.remove(stack[-1])

    for i, adjacency in enumerate(graph[stack[-1]]):
        if adjacency == 1 and i in yet:  # 隣接かつ未探索であれば、stackにpush
            stack.append(i)
            dfs(stack)
    else:
        ct += 1
        f[(stack[-1]-1)] = ct
        stack.pop()


if __name__ == "__main__":
    main()

Python: n = int(input())
M = [[0]*n for _ in range(n)]

for _ in range(n):
    u, k, *V = map(int, input().split())
    if k == 0:
        continue
    else:
        for v in V:
            M[u-1][v-1] = 1

color = ['White']*n

time = 0
d = [0]*n
f = [0]*n
def dfs_visit(u=0):
    global time
    color[u] = 'Gray'
    time += 1
    d[u] = time
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == 'White':
            dfs_visit(v)
    color[u] = "Black"
    time += 1
    f[u] = time

def dfs():
    for u in range(n):
        if color[u] == 'White':
            dfs_visit(u)
    for u in range(n):
        print('{} {} {}'.format(u+1, d[u], f[u]))


dfs()

Python: # coding: utf-8
# Your code here!

import sys
read = sys.stdin.read
readline = sys.stdin.readline


def dfs(G, v, d, f, time):
    time += 1
    d[v] = time

    for e in G[v]:
        if d[e] == 0:
            time = dfs(G, e, d, f, time)

    time += 1
    f[v] = time

    return time


def main():
    n = int(readline())

    E = [[]]
    for _ in range(n):
        u,k,*v = map(int, readline().split())
        E.append(v)

    d = [0] * (n+1)
    f = [0] * (n+1)
    time = 0
    
    for i in range(1,n+1):
        if d[i] == 0:
            time = dfs(E, i, d, f, time)
    
    for i in range(1, n+1):
        print(i, d[i], f[i])


if __name__ == "__main__":
    main()