Go: package main

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

/***************************************************************
stackによる深さ優先探索を実装する。
***************************************************************/

const (
	N     = 100
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

var (
	n            int
	Adj          [N][N]int
	statusColor  [N]int
	discoverTime [N]int
	finishTime   [N]int
	clock        int
	nt           [N]int
)

/***************************************************************
Stackの定義:
- field
	container: ノードの格納されたスライス
	sp: スタックポインタ
- method
	size(): スタックに積まれているノードの個数を返す
	top(): スタックの一番上に積まれているノードを返す
	isEmpty(): スタックが空かどうかを返す
	pop(): スタックの要素をポップし削除する
	push(): スタックに要素を積む
***************************************************************/

type Stack struct {
	container []int
	sp        int
}

func (s *Stack) size() int {
	return len(s.container)
}

func (s *Stack) top() int {
	return s.container[s.sp]
}

func (s *Stack) isEmpty() bool {
	return s.sp == 0
}

func (s *Stack) pop() int {
	_pop := func() {
		s.container = s.container[:len(s.container)-1]
	}
	defer _pop()
	if s.isEmpty() {
		panic("There is no elements in stack!")
	}
	s.sp--
	return s.container[s.sp]
}

func (s *Stack) push(n int) {
	s.sp++
	s.container[s.sp] = n
}

func next(u int) int {
	for v := nt[u]; v < n; v++ {
		nt[u] = v + 1
		if Adj[u][v] == 1 {
			return v
		}
	}
	return -1
}

func _DFS(target int) {
	for i := 0; i < n; i++ {
		nt[i] = 0
	}
	stack := new(Stack)
	stack.container = make([]int, N+1)
	stack.push(target)
	statusColor[target] = GRAY
	clock++
	discoverTime[target] = clock
	for !stack.isEmpty() {
		u := stack.top()
		v := next(u)
		if v != -1 {
			if statusColor[v] == WHITE {
				statusColor[v] = GRAY
				clock++
				discoverTime[v] = clock
				stack.push(v)
			}
		} else {
			stack.pop()
			statusColor[u] = BLACK
			clock++
			finishTime[u] = clock
		}
	}
}

func DFS() {
	for i := 0; i < n; i++ {
		statusColor[i] = WHITE
		nt[i] = 0
	}
	clock = 0
	for i := 0; i < n; i++ {
		if statusColor[i] == WHITE {
			_DFS(i)
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d %d\n", i+1, discoverTime[i], finishTime[i])
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			Adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		u--
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ := strconv.Atoi(scanner.Text())
			v--
			Adj[u][v] = 1
		}
	}

	DFS()
}

Python: #!/usr/bin/env python3
import sys
sys.setrecursionlimit(15000)
n = int(sys.stdin.readline())

ret = []
inp = sys.stdin.readlines()
G = [None]*(n)
d = [0]*(n)
f = [0]*(n)
def dfs(i,t):
  if d[i] != 0:
    return t
  t = t+1
  d[i] = t
  for j in G[i]:
    t = dfs(j,t)
  f[i] = t+1
  return f[i]
for i in range(n):
  v = list(map(int,inp[i].split()))
  G[v[0]-1] = list(map(lambda x: int(x-1),v[2:]))
#print(G)
t = 0
for j in range(n):
  t = dfs(j,t)

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

Python: N = int(input())
UKV = [list(map(int,input().split())) for _ in range(N)]

seen = [False]*N
first_time = [-1]*N
last_time = [-1]*N
time = 0
def dfs(G, v):
    global seen
    global first_time
    global last_time
    global time
    seen[v-1] = True
    time += 1
    first_time[v-1] = time
    for next_v in G[v]:
        if seen[next_v-1]:
            continue
        dfs(G, next_v)
    time += 1
    last_time[v-1] = time

G = {}
for ukv in UKV:
    G[ukv[0]] = sorted(ukv[2:])
for i in range(1,N+1):
    if seen[i-1]:
        continue
    dfs(G, i)

for i in range(1,N+1):
    print(i,first_time[i-1],last_time[i-1])

Python: G = {}
for _ in range(int(input())):
  x = list(map(int, input().split()))
  G[x[0]] = sorted(x[2:])

t = 1
stack = [1]
timestamp = {1:[t]}

while len(stack) > 0:
  t += 1
  v = stack[-1]

  while len(G[v]) > 0:
    v = G[v].pop(0)
    if v in timestamp: continue
    stack.append(v)
    timestamp[v] = [t]
    break
  else:
    v = stack.pop()
    timestamp[v].append(t)

for i in timestamp:
  print(i, *timestamp[i])

Python: import sys

time = 0
def dfs(u):
    global time
    flag_list[u] = 1
    time += 1
    d_list[u] = time
    for v in range(n):
        if m_list[u][v] == 1 and flag_list[v] == 0:
            dfs(v)
    flag_list[u] = 2
    time += 1
    f_list[u] = time


input = sys.stdin.readline
n = int(input())
m_list = [[0]*n for _ in range(n)]
for _ in range(n):
    input_list = list(map(int, input().split()))
    index = input_list[0] - 1
    if input_list[1] > 0:
        for a in input_list[2:]:
            m_list[index][a-1] = 1
    
d_list = [0]*n
f_list = [0]*n
flag_list = [0]*n # 0: 未到達, 1: 処理中, 2: 完了
for i in range(n):
    if flag_list[i] == 0:
        dfs(i)

for i in range(n):
    print(i+1, d_list[i], f_list[i])

Python: G = {}
N = int(input())
for _ in range(N):
    L = list(map(int, input().split()))
    G[L[0]] = L[2:]

# print(G)
d = [-1] * N
f = [-1] * N
q = [1]
visited = [False] * N

time = 1


def dfs(v):
    global time
    visited[v-1] = True

    for nv in G[v]:
        if d[nv-1] < 0:
            d[nv-1] = time
            time += 1
        if not visited[nv-1]:
            dfs(nv)

    f[v-1] = time
    time += 1


for i in range(1, N+1):
    if not visited[i-1]:
        d[i-1] = time
        time += 1
        dfs(i)

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

Python: import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect


# from operator import mul

# sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = getSomeInputListInt(n)

ans = [[] for _ in range(n)]

edge = []
for d in data:
    edge.append(d[2:])


def dfs(i, cnt):
    if len(ans[i]) > 0:
        return cnt

    cnt += 1

    ans[i].append(cnt)
    for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

    cnt += 1
    ans[i].append(cnt)

    return cnt

tmp = 0
for i in range(n):
    tmp = dfs(i, tmp)

for i in range(n):
    print(i + 1, *ans[i], sep=" ")

Python: class Node:
    def __init__(self, data):
        self.data = data
        self.neighbors = list()
        self.find_time = 0
        self.end_time = 0

N = int(input())
work_id = 0
nodes = list()
nodes.append(Node(0))


for _ in range(N):
    tmp_list = list( map(int, input().split()) )
    node = Node(tmp_list[0])
    for i in range(tmp_list[1]):
        node.neighbors.append(tmp_list[i+2])
    nodes.append(node)
    nodes[0].neighbors.append(node.data)

# for node in nodes:
#     print(node.data, ":", node.neighbors)


def search(node):
    global work_id

    if node.find_time != 0:
        return
    
    if node.data != 0:
        work_id += 1
        node.find_time = work_id

    if len(node.neighbors) > 0:
        for neighbor in node.neighbors:
            search(nodes[neighbor])
    
    if node.data != 0:
        work_id += 1
        node.end_time = work_id

search(nodes[0])

for node in nodes:
    if node.data == 0: continue
    print(node.data, node.find_time, node.end_time)