import sys
sys.setrecursionlimit(10**6)

n = int(input())
graph = []
chart = [[int(i)] for i in range(1,n+1)]
for i in range(n):
    z = [c-1 for c in list(map(int,input().split()))][2:]
    graph.append(z)
time = 0

def dfs(nod):
    global graph
    global time
    #break condition
    if len(chart[nod]) != 1:
        return 
    #end condition not necessary
    #if not graph[nod]:
    #    time += 1
    #    chart[nod].append(time)
    #    return
    # command
    time += 1
    chart[nod].append(time)
    
    # move
    for dis in graph[nod]:
            dfs(dis)

    # post-command
    time += 1
    chart[nod].append(time)
for i in range(n):
    dfs(i)
for i in range(n):
    print(*chart[i])
 import sys sys sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 n = int(input()) n int(input()) int input() input graph = [] graph [] [int(i)] for i in range(1,n+1) for i in range(1,n+1) i range(1,n+1) range 1 n+1 n 1 for i in range(1,n+1) [int(i)] int(i) int i chart = [[int(i)] for i in range(1,n+1)] chart [[int(i)] for i in range(1,n+1)] for i in range(n):
    z = [c-1 for c in list(map(int,input().split()))][2:]
    graph.append(z) i range(n) range n c-1 for c in list(map(int,input().split())) for c in list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for c in list(map(int,input().split())) c-1 c 1 z = [c-1 for c in list(map(int,input().split()))][2:] z [c-1 for c in list(map(int,input().split()))][2:] [c-1 for c in list(map(int,input().split()))] 2: 2 graph.append(z) graph.append graph append z time = 0 time 0 def dfs(nod):
    global graph
    global time
    #break condition
    if len(chart[nod]) != 1:
        return 
    #end condition not necessary
    #if not graph[nod]:
    #    time += 1
    #    chart[nod].append(time)
    #    return
    # command
    time += 1
    chart[nod].append(time)
    
    # move
    for dis in graph[nod]:
            dfs(dis)

    # post-command
    time += 1
    chart[nod].append(time) dfs global graph global time if len(chart[nod]) != 1:
        return 
    #end condition not necessary
    #if not graph[nod]:
    #    time += 1
    #    chart[nod].append(time)
    #    return
    # command
     len(chart[nod]) != 1 len(chart[nod]) len chart[nod] chart nod 1 return time += 1 time 1 chart[nod].append(time) [nod].append [nod] chart nod append time for dis in graph[nod]:
            dfs(dis)

    # post-command
     dis graph[nod] graph nod dfs(dis) dfs dis time += 1 time 1 chart[nod].append(time) [nod].append [nod] chart nod append time nod nod for i in range(n):
    dfs(i) i range(n) range n dfs(i) dfs i for i in range(n):
    print(*chart[i]) i range(n) range n print(*chart[i]) print *chart[i] chart[i] chart i chart = [[int(i)] for i in range(1,n+1)] [[int(i)] for i in range(1,n+1)] chart n = int(input()) int(input()) n graph = [] [] graph time = 0 0 time z = [c-1 for c in list(map(int,input().split()))][2:] [c-1 for c in list(map(int,input().split()))][2:] z def dfs(nod):
    global graph
    global time
    #break condition
    if len(chart[nod]) != 1:
        return 
    #end condition not necessary
    #if not graph[nod]:
    #    time += 1
    #    chart[nod].append(time)
    #    return
    # command
    time += 1
    chart[nod].append(time)
    
    # move
    for dis in graph[nod]:
            dfs(dis)

    # post-command
    time += 1
    chart[nod].append(time) def dfs(nod):
    global graph
    global time
    #break condition
    if len(chart[nod]) != 1:
        return 
    #end condition not necessary
    #if not graph[nod]:
    #    time += 1
    #    chart[nod].append(time)
    #    return
    # command
    time += 1
    chart[nod].append(time)
    
    # move
    for dis in graph[nod]:
            dfs(dis)

    # post-command
    time += 1
    chart[nod].append(time) dfs
import sys
sys.setrecursionlimit(10**6)

n = int(input())
graph = []
chart = [[int(i)] for i in range(1,n+1)]
for i in range(n):
    z = [c-1 for c in list(map(int,input().split()))][2:]
    graph.append(z)
time = 0

def dfs(nod):
    global graph
    global time
    #break condition
    if len(chart[nod]) != 1:
        return 
    #end condition not necessary
    #if not graph[nod]:
    #    time += 1
    #    chart[nod].append(time)
    #    return
    # command
    time += 1
    chart[nod].append(time)
    
    # move
    for dis in graph[nod]:
            dfs(dis)

    # post-command
    time += 1
    chart[nod].append(time)
for i in range(n):
    dfs(i)
for i in range(n):
    print(*chart[i])

