from collections import deque

seen=[]
first_order =[]
last_order = []

def dfs(g, v, first_ptr, last_ptr):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True 
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr

icase=1
if icase==0:
    n,m=map(int,input().split())
    g = [deque([]) for _ in range(n + 1)]
    for i in range(m):
        a,b=map(int,input().split())
        g[a].append(b)
#        g[b].append(a) # 無向グラフ
elif icase==1:        
    n=int(input())
    u=[0]*n
    k=[0]*n
#    v=[[] for i in range(n)]
    g = [deque([]) for _ in range(n + 1)]
    for i in range(n):
        ai=list(map(int,input().split()))
        u[i]=ai[0]
        k[i]=ai[1]
        g[i]=deque(ai[2:])
        for j in range(len(g[i])):
            g[i][j]-=1    
elif icase==2:
    n=6
    u=[1,2,3,4,5,6]
    k=[2,2,1,1,1,0]
    g = [deque([]) for _ in range(n + 1)]

    g[0]=deque([1,2])
    g[1]=deque([2,3])
    g[2]=deque([4])
    g[3]=deque([5])
    g[4]=deque([5])
    g[5]=deque([])
    g[5]=deque()

seen=[False]*n
first_ptr=0
last_ptr=0
first_order =[-1]*n
last_order = [-1]*n

for v in range(n):
    if seen[v]:
        continue
    else:
        first_ptr, last_ptr=dfs(g,v,first_ptr, last_ptr)

for v in range(n):
    print(v+1,first_order[v],last_order[v]) 
     from collections import deque seen=[] seen [] first_order =[] first_order [] last_order = [] last_order [] def dfs(g, v, first_ptr, last_ptr):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True 
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr dfs first_ptr=first_ptr+1 first_ptr first_ptr+1 first_ptr 1 last_ptr=first_ptr last_ptr first_ptr first_order[v] = first_ptr first_order[v] first_order v first_ptr seen[v]=True seen[v] seen v True for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr)
     next_v g[v] g v if seen[next_v]:
            continue
         seen[next_v] seen next_v continue first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr) first_ptr dfs(g, next_v, first_ptr, last_ptr) dfs g next_v first_ptr last_ptr last_ptr last_ptr=last_ptr+1 last_ptr last_ptr+1 last_ptr 1 first_ptr=last_ptr first_ptr last_ptr last_order[v] = last_ptr last_order[v] last_order v last_ptr return first_ptr, last_ptr return first_ptr, last_ptr first_ptr last_ptr g g v v first_ptr first_ptr last_ptr last_ptr icase=1 icase 1 if icase==0:
    n,m=map(int,input().split())
    g = [deque([]) for _ in range(n + 1)]
    for i in range(m):
        a,b=map(int,input().split())
        g[a].append(b)
#        g[b].append(a) # 無向グラフ
elif icase==1:        
    n=int(input())
    u=[0]*n
    k=[0]*n
#    v=[[] for i in range(n)]
    g = [deque([]) for _ in range(n + 1)]
    for i in range(n):
        ai=list(map(int,input().split()))
        u[i]=ai[0]
        k[i]=ai[1]
        g[i]=deque(ai[2:])
        for j in range(len(g[i])):
            g[i][j]-=1    
elif icase==2:
    n=6
    u=[1,2,3,4,5,6]
    k=[2,2,1,1,1,0]
    g = [deque([]) for _ in range(n + 1)]

    g[0]=deque([1,2])
    g[1]=deque([2,3])
    g[2]=deque([4])
    g[3]=deque([5])
    g[4]=deque([5])
    g[5]=deque([])
    g[5]=deque() icase==0 icase 0 n,m=map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split m deque([]) for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) deque([]) deque [] g = [deque([]) for _ in range(n + 1)] g [deque([]) for _ in range(n + 1)] for i in range(m):
        a,b=map(int,input().split())
        g[a].append(b)
#        g[b].append(a) # 無向グラフ i range(m) range m a,b=map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b g[a].append(b) [a].append [a] g a append b elif icase==1:        
    n=int(input())
    u=[0]*n
    k=[0]*n
#    v=[[] for i in range(n)]
    g = [deque([]) for _ in range(n + 1)]
    for i in range(n):
        ai=list(map(int,input().split()))
        u[i]=ai[0]
        k[i]=ai[1]
        g[i]=deque(ai[2:])
        for j in range(len(g[i])):
            g[i][j]-=1     icase==1 icase 1 n=int(input()) n int(input()) int input() input u=[0]*n u [0]*n [0] 0 n k=[0]*n k [0]*n [0] 0 n deque([]) for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) deque([]) deque [] g = [deque([]) for _ in range(n + 1)] g [deque([]) for _ in range(n + 1)] for i in range(n):
        ai=list(map(int,input().split()))
        u[i]=ai[0]
        k[i]=ai[1]
        g[i]=deque(ai[2:])
        for j in range(len(g[i])):
            g[i][j]-=1     i range(n) range n ai=list(map(int,input().split())) ai list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split u[i]=ai[0] u[i] u i ai[0] ai 0 k[i]=ai[1] k[i] k i ai[1] ai 1 g[i]=deque(ai[2:]) g[i] g i deque(ai[2:]) deque ai[2:] ai 2: 2 for j in range(len(g[i])):
            g[i][j]-=1     j range(len(g[i])) range len(g[i]) len g[i] g i g[i][j]-=1 g[i][j] [i] g i j 1 elif icase==2:
    n=6
    u=[1,2,3,4,5,6]
    k=[2,2,1,1,1,0]
    g = [deque([]) for _ in range(n + 1)]

    g[0]=deque([1,2])
    g[1]=deque([2,3])
    g[2]=deque([4])
    g[3]=deque([5])
    g[4]=deque([5])
    g[5]=deque([])
    g[5]=deque() icase==2 icase 2 n=6 n 6 u=[1,2,3,4,5,6] u [1,2,3,4,5,6] 1 2 3 4 5 6 k=[2,2,1,1,1,0] k [2,2,1,1,1,0] 2 2 1 1 1 0 deque([]) for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) deque([]) deque [] g = [deque([]) for _ in range(n + 1)] g [deque([]) for _ in range(n + 1)] g[0]=deque([1,2]) g[0] g 0 deque([1,2]) deque [1,2] 1 2 g[1]=deque([2,3]) g[1] g 1 deque([2,3]) deque [2,3] 2 3 g[2]=deque([4]) g[2] g 2 deque([4]) deque [4] 4 g[3]=deque([5]) g[3] g 3 deque([5]) deque [5] 5 g[4]=deque([5]) g[4] g 4 deque([5]) deque [5] 5 g[5]=deque([]) g[5] g 5 deque([]) deque [] g[5]=deque() g[5] g 5 deque() deque seen=[False]*n seen [False]*n [False] False n first_ptr=0 first_ptr 0 last_ptr=0 last_ptr 0 first_order =[-1]*n first_order [-1]*n [-1] -1 n last_order = [-1]*n last_order [-1]*n [-1] -1 n for v in range(n):
    if seen[v]:
        continue
    else:
        first_ptr, last_ptr=dfs(g,v,first_ptr, last_ptr) v range(n) range n if seen[v]:
        continue
    else:
        first_ptr, last_ptr=dfs(g,v,first_ptr, last_ptr) seen[v] seen v continue first_ptr, last_ptr=dfs(g,v,first_ptr, last_ptr) first_ptr dfs(g,v,first_ptr, last_ptr) dfs g v first_ptr last_ptr last_ptr for v in range(n):
    print(v+1,first_order[v],last_order[v]) 
     v range(n) range n print(v+1,first_order[v],last_order[v]) print v+1 v 1 first_order[v] first_order v last_order[v] last_order v u=[1,2,3,4,5,6] [1,2,3,4,5,6] u def dfs(g, v, first_ptr, last_ptr):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True 
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr def dfs(g, v, first_ptr, last_ptr):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True 
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr dfs k=[2,2,1,1,1,0] [2,2,1,1,1,0] k first_ptr, last_ptr=dfs(g,v,first_ptr, last_ptr) dfs(g,v,first_ptr, last_ptr) first_ptr a,b=map(int,input().split()) map(int,input().split()) a seen=[] [] seen n=int(input()) int(input()) n last_order = [] [] last_order g = [deque([]) for _ in range(n + 1)] [deque([]) for _ in range(n + 1)] g seen=[False]*n [False]*n seen last_ptr=dfs(g,v,first_ptr, last_ptr) dfs(g,v,first_ptr, last_ptr) last_ptr n,m=map(int,input().split()) map(int,input().split()) n last_ptr=0 0 last_ptr ai=list(map(int,input().split())) list(map(int,input().split())) ai b=map(int,input().split()) map(int,input().split()) b last_order = [-1]*n [-1]*n last_order g = [deque([]) for _ in range(n + 1)] [deque([]) for _ in range(n + 1)] g icase=1 1 icase first_order =[] [] first_order first_order =[-1]*n [-1]*n first_order k=[0]*n [0]*n k g = [deque([]) for _ in range(n + 1)] [deque([]) for _ in range(n + 1)] g u=[0]*n [0]*n u m=map(int,input().split()) map(int,input().split()) m n=6 6 n first_ptr=0 0 first_ptr
from collections import deque

seen=[]
first_order =[]
last_order = []

def dfs(g, v, first_ptr, last_ptr):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True 
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr

icase=1
if icase==0:
    n,m=map(int,input().split())
    g = [deque([]) for _ in range(n + 1)]
    for i in range(m):
        a,b=map(int,input().split())
        g[a].append(b)
#        g[b].append(a) # 無向グラフ
elif icase==1:        
    n=int(input())
    u=[0]*n
    k=[0]*n
#    v=[[] for i in range(n)]
    g = [deque([]) for _ in range(n + 1)]
    for i in range(n):
        ai=list(map(int,input().split()))
        u[i]=ai[0]
        k[i]=ai[1]
        g[i]=deque(ai[2:])
        for j in range(len(g[i])):
            g[i][j]-=1    
elif icase==2:
    n=6
    u=[1,2,3,4,5,6]
    k=[2,2,1,1,1,0]
    g = [deque([]) for _ in range(n + 1)]

    g[0]=deque([1,2])
    g[1]=deque([2,3])
    g[2]=deque([4])
    g[3]=deque([5])
    g[4]=deque([5])
    g[5]=deque([])
    g[5]=deque()

seen=[False]*n
first_ptr=0
last_ptr=0
first_order =[-1]*n
last_order = [-1]*n

for v in range(n):
    if seen[v]:
        continue
    else:
        first_ptr, last_ptr=dfs(g,v,first_ptr, last_ptr)

for v in range(n):
    print(v+1,first_order[v],last_order[v]) 
    
