Go: package main

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

type TimeRecord struct {
	discoverTime int
	finishTime   int
}

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

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

	timeRecordMap := make([]TimeRecord, n)
	childrenMap := make([][]int, n)
	for i := 1; i <= n; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		timeRecordMap[node-1] = TimeRecord{-1, -1}

		childNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		childrenMap[node-1] = make([]int, childNum)
		for j := 0; j < childNum; j++ {
			child, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			childrenMap[node-1][j] = child
		}
	}

	timer := 1
	for {
		startNode := findStartNode(childrenMap, timeRecordMap)
		if startNode > 0 {
			visit(startNode, &timer, childrenMap, timeRecordMap)
		} else {
			break
		}
	}
	for k, v := range timeRecordMap {
		fmt.Printf("%d %d %d\n", k+1, v.discoverTime, v.finishTime)
	}
}

func findStartNode(childrenMap [][]int, timeRecordMap []TimeRecord) int {
	x := -1
	for node, timeRecord := range timeRecordMap {
		if timeRecord.discoverTime < 0 {
			x = node + 1
			break
		}
	}
	if x < 0 {
		return -1
	}

	found := false
	i := 0
	for i < len(childrenMap) {
		children := childrenMap[i]
		for j := 0; j < len(children); j++ {
			if children[j] == x {
				x = j + 1
				found = true
				break
			}
		}
		if found {
			i = 0
		} else {
			i++
		}
	}
	return x
}

func visit(currentNode int, timer *int, childrenMap [][]int, timeRecordMap []TimeRecord) {
	if timeRecordMap[currentNode-1].discoverTime > 0 {
		return
	}
	children := childrenMap[currentNode-1]
	timeRecordMap[currentNode-1] = TimeRecord{*timer, timeRecordMap[currentNode-1].finishTime}
	(*timer)++
	for i := 0; i < len(children); i++ {
		visit(children[i], timer, childrenMap, timeRecordMap)
	}
	timeRecordMap[currentNode-1] = TimeRecord{timeRecordMap[currentNode-1].discoverTime, *timer}
	(*timer)++
}

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

Python: N = int(input())

from collections import deque,defaultdict
G = defaultdict(deque)
for i in range(1,N+1):
    t = [int(i) for i in input().split(' ')]
    for j in range(t[1]):
        G[t[0]].append(t[j+2])

time = 1
arrive_time = [-1] * (N + 1) # 到着時刻
finish_time = [-1] * (N + 1) # 到着時刻

def dfs(G,s):
    global time
    stack = deque([s])
    arrive_time[s] = time
    while stack:
        t = stack[-1]
        if G[t]:
            w = G[t].popleft()
            if arrive_time[w] >= 0:
                continue
            elif w in stack:
                continue
            else:
                time += 1
                arrive_time[w] = time
                stack.append(w)
        else:
            time += 1
            finish_time[t] = time
            stack.pop()
            
    return arrive_time,finish_time

for i in range(1,N+1):
    if arrive_time[i] < 0:
        arrive,finish = dfs(G,i)
        time += 1

for i, (a, f) in enumerate(zip(arrive,finish)):
    if not i == 0:
        print(i,a,f)

Python: import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

global current_time
global visited,go_time,back_time
global G


def dfs(node_id):
    global current_time #書かないとローカル変数扱いされる
    go_time[node_id] = current_time
    current_time += 1

    for next_node in G[node_id]:

        if visited[next_node] == True:
            continue
        visited[next_node] = True
        dfs(next_node)

    back_time[node_id] = current_time
    current_time += 1


V = int(input())

G = [[] for i in range(V)]

visited = [False]*V

go_time = [0]*V
back_time = [0]*V

for loop in range(V):
    node_id,num,*tmp_adj = list(map(int,input().split()))
    node_id -= 1
    for i in range(num):
        G[node_id].append(tmp_adj[i]-1)

current_time = 1;

for i in range(V):
    if visited[i] == True:
        continue
    visited[i] = True
    dfs(i)

for i in range(V):
    print("%d %d %d"%(i+1,go_time[i],back_time[i]))

Python: n = int(input())
g = []
for i in range(n):
    a = list(map(int,input().split()))
    g.append(a[2:])
d = [0]*n
f = [0]*n
global t
t = 1
d[0] = 1
x = 1
def find(x):
    global t
    if len(g[x-1]) == 0:
        t += 1
        f[x-1] = t
    else:
        for i in g[x-1]:
            if d[i-1] == 0:
                t += 1
                d[i-1] = t
                find(i)
        t += 1
        f[x-1] = t

find(x)
for i in range(n):
    if d[i] == 0:
        t += 1
        d[i] = t
        find(i+1)
for i in range(n):
    print(i+1, d[i], f[i])

Python: def depth(s, time):
    time += 1
    flag[s] = 1
    d[s] = t
    for i in range(1, n+1):
        if G[s][i] == 1:
            if flag[i] == 0:
                depth(i, time)
                t = 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: n = int(input())
adj = [[None for _ in range(n)] for _ in range(n)]
for i in range(n):
    l = list(map(int, input().split()))
    if l[1] == 0:
        continue 
    else:
        for j in range(2,len(l)):
            adj[l[0]-1][l[j]-1] = 1
color = ['white' for _ in range(n)]
d = [None for _ in range(n)]
f = [None for _ in range(n)]
time = 1

def dfs(node):
    global color
    global d 
    global f
    global time 
    S = [node]
    color[node -1] = 'grey'
    d[node -1] = time 
    while S:
        u = S[-1]
        v = 1
        while v <= n:
            if adj[u-1][v-1] == 1 and color[v-1] == 'white':
                break
            v += 1
        if v < n+1:
            color[v-1] = 'grey'
            time += 1
            d[v-1] = time 
            S.append(v)
        else:
            time += 1
            S.pop()
            color[u-1] = ['black']
            f[u-1] = time

for node in range(1, n+1):
    if color[node-1] == 'white':
        dfs(node)
        time += 1
for i in range(n):
    print(' '.join([str(i+1), str(d[i]), str(f[i])]))

Python: ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

class tree:
    def __init__(self,n):
        self.n = n
        self.vlist = [None]*self.n
        self.start = [None]*self.n
        self.finish = [None]*self.n
        self.time = 0
    
    def add(self,n,v):
        v.sort()
        self.vlist[n] = v

    def search(self,u):
        if self.start[u] != None:
            return
        else:
            self.time += 1
            self.start[u] = self.time

            for i in self.vlist[u]:
                self.search(i-1)

            self.time += 1
            self.finish[u] = self.time
            return

n = ii()
t = tree(n)

for i in range(n):
    vk = li()
    u = vk[0]
    k = vk[1]
    v = vk[2:]
    t.add(u-1,v)

for i in range(n):
    t.search(i)

for i in range(n):
    print(i+1, t.start[i], t.finish[i])