n=int(input())
a=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
    v=list(input().split())
    for j in range(len(v)):
        v[j]=int(v[j])
    x=2
    for k in range(v[1]):
        a[i][v[x+k]-1]=1
#print(a)

stack=[]
v=[]
d=[0]*n
f=[0]*n
t=1

while len(v)<n:
    for i in range(n):
        if i+1 not in v:
            stack.append(i+1)
            d[i]=t
            break
        else: pass
        
    while len(stack)!=0:
        for i in range(n):
            if a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v and i+1 not in stack:
                stack.append(i+1)
                t+=1
                d[i]=t
                break
            elif i==n-1:
                t+=1
                f[stack[-1]-1]=t
                v.append(stack.pop())
            else: pass
            #print("i=",i)
            #print("stack ",stack)
            #print("v ",v)
    t+=1
#print("d=",d," f=",f)
for i in range(n):
    print(i+1,d[i],f[i])
 n=int(input()) n int(input()) int input() input [0 for i in range(n)]for j in range(n) for j in range(n) j range(n) range n for j in range(n) [0 for i in range(n)] a=[[0 for i in range(n)]for j in range(n)] a [[0 for i in range(n)]for j in range(n)] for i in range(n):
    v=list(input().split())
    for j in range(len(v)):
        v[j]=int(v[j])
    x=2
    for k in range(v[1]):
        a[i][v[x+k]-1]=1
#print(a) i range(n) range n v=list(input().split()) v list(input().split()) list input().split() ().split () input split for j in range(len(v)):
        v[j]=int(v[j])
     j range(len(v)) range len(v) len v v[j]=int(v[j]) v[j] v j int(v[j]) int v[j] v j x=2 x 2 for k in range(v[1]):
        a[i][v[x+k]-1]=1
#print(a) k range(v[1]) range v[1] v 1 a[i][v[x+k]-1]=1 a[i][v[x+k]-1] [i] a i v[x+k]-1 v[x+k] v x+k x k 1 1 stack=[] stack [] v=[] v [] d=[0]*n d [0]*n [0] 0 n f=[0]*n f [0]*n [0] 0 n t=1 t 1 while len(v)<n:
    for i in range(n):
        if i+1 not in v:
            stack.append(i+1)
            d[i]=t
            break
        else: pass
        
    while len(stack)!=0:
        for i in range(n):
            if a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v and i+1 not in stack:
                stack.append(i+1)
                t+=1
                d[i]=t
                break
            elif i==n-1:
                t+=1
                f[stack[-1]-1]=t
                v.append(stack.pop())
            else: pass
            #print("i=",i)
            #print("stack ",stack)
            #print("v ",v)
    t+=1
#print("d=",d," f=",f) len(v)<n len(v) len v n for i in range(n):
        if i+1 not in v:
            stack.append(i+1)
            d[i]=t
            break
        else: pass
        
     i range(n) range n if i+1 not in v:
            stack.append(i+1)
            d[i]=t
            break
        else: pass
        
     i+1 not in v i+1 i 1 v stack.append(i+1) stack.append stack append i+1 i 1 d[i]=t d[i] d i t break while len(stack)!=0:
        for i in range(n):
            if a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v and i+1 not in stack:
                stack.append(i+1)
                t+=1
                d[i]=t
                break
            elif i==n-1:
                t+=1
                f[stack[-1]-1]=t
                v.append(stack.pop())
            else: pass
            #print("i=",i)
            #print("stack ",stack)
            #print("v ",v)
     len(stack)!=0 len(stack) len stack 0 for i in range(n):
            if a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v and i+1 not in stack:
                stack.append(i+1)
                t+=1
                d[i]=t
                break
            elif i==n-1:
                t+=1
                f[stack[-1]-1]=t
                v.append(stack.pop())
            else: pass
            #print("i=",i)
            #print("stack ",stack)
            #print("v ",v)
     i range(n) range n if a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v and i+1 not in stack:
                stack.append(i+1)
                t+=1
                d[i]=t
                break
            elif i==n-1:
                t+=1
                f[stack[-1]-1]=t
                v.append(stack.pop())
            else: pass
            #print("i=",i)
            #print("stack ",stack)
            #print("v ",v)
     a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v and i+1 not in stack a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v a[stack[-1]-1][i]==1 and stack[-1]-1!=i a[stack[-1]-1][i]==1 a[stack[-1]-1][i] [stack[-1]-1] a stack[-1]-1 stack[-1] stack -1 1 i 1 stack[-1]-1!=i stack[-1]-1 stack[-1] stack -1 1 i i+1 not in v i+1 i 1 v i+1 not in stack i+1 i 1 stack stack.append(i+1) stack.append stack append i+1 i 1 t+=1 t 1 d[i]=t d[i] d i t break elif i==n-1:
                t+=1
                f[stack[-1]-1]=t
                v.append(stack.pop())
             i==n-1 i n-1 n 1 t+=1 t 1 f[stack[-1]-1]=t f[stack[-1]-1] f stack[-1]-1 stack[-1] stack -1 1 t v.append(stack.pop()) v.append v append stack.pop() stack.pop stack pop t+=1 t 1 for i in range(n):
    print(i+1,d[i],f[i]) i range(n) range n print(i+1,d[i],f[i]) print i+1 i 1 d[i] d i f[i] f i d=[0]*n [0]*n d v=list(input().split()) list(input().split()) v n=int(input()) int(input()) n x=2 2 x t+=1 1 t t+=1 1 t t=1 1 t a=[[0 for i in range(n)]for j in range(n)] [[0 for i in range(n)]for j in range(n)] a v=[] [] v stack=[] [] stack f=[0]*n [0]*n f t+=1 1 t
n=int(input())
a=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
    v=list(input().split())
    for j in range(len(v)):
        v[j]=int(v[j])
    x=2
    for k in range(v[1]):
        a[i][v[x+k]-1]=1
#print(a)

stack=[]
v=[]
d=[0]*n
f=[0]*n
t=1

while len(v)<n:
    for i in range(n):
        if i+1 not in v:
            stack.append(i+1)
            d[i]=t
            break
        else: pass
        
    while len(stack)!=0:
        for i in range(n):
            if a[stack[-1]-1][i]==1 and stack[-1]-1!=i and i+1 not in v and i+1 not in stack:
                stack.append(i+1)
                t+=1
                d[i]=t
                break
            elif i==n-1:
                t+=1
                f[stack[-1]-1]=t
                v.append(stack.pop())
            else: pass
            #print("i=",i)
            #print("stack ",stack)
            #print("v ",v)
    t+=1
#print("d=",d," f=",f)
for i in range(n):
    print(i+1,d[i],f[i])

