n=int(input())
G=[[j for j in range(n)] for i in range(n)]
for i in range(n):
    g_in=list(map(int,input().split()))
    u=g_in[0]
    k=g_in[1]
    v=g_in[2:]
    for j in v:
        G[u-1][j-1]=1

visited=[0 for i in range(n)]
finished=[0 for i in range(n)]
L=[]
out=[[i+1,0,0] for i in range(n)]
step=0

def dfs(cn):
    flg=0
    if visited[cn]==0:
        flg=1
        L.append(cn)
    visited[cn]=1
    for i in range(n):
        if cn!=i and G[cn][i]==1 and visited[i]==0:
            dfs(i)
    if flg==1:
        L.append(cn)

i=0
while visited.count(1)<n:
    dfs(i)
    i+=1
copied=[0 for i in range(len(L))]

for i in range(len(L)):
    if copied[L[i]]==0:
        copied[L[i]]=1
        out[L[i]][1]=i+1
    elif copied[L[i]]==1:
        copied[L[i]]=2
        out[L[i]][2]=i+1

out=[' '.join(map(str,i)) for i in out]
#print(' '.join(map(str,[i+1 for i in L])))
print('\n'.join(map(str,out))) n=int(input()) n int(input()) int input() input [j for j in range(n)] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [j for j in range(n)] G=[[j for j in range(n)] for i in range(n)] G [[j for j in range(n)] for i in range(n)] for i in range(n):
    g_in=list(map(int,input().split()))
    u=g_in[0]
    k=g_in[1]
    v=g_in[2:]
    for j in v:
        G[u-1][j-1]=1 i range(n) range n g_in=list(map(int,input().split())) g_in list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split u=g_in[0] u g_in[0] g_in 0 k=g_in[1] k g_in[1] g_in 1 v=g_in[2:] v g_in[2:] g_in 2: 2 for j in v:
        G[u-1][j-1]=1 j v G[u-1][j-1]=1 G[u-1][j-1] [u-1] G u-1 u 1 j-1 j 1 1 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 visited=[0 for i in range(n)] visited [0 for i in range(n)] 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 finished=[0 for i in range(n)] finished [0 for i in range(n)] L=[] L [] [i+1,0,0] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [i+1,0,0] i+1 i 1 0 0 out=[[i+1,0,0] for i in range(n)] out [[i+1,0,0] for i in range(n)] step=0 step 0 def dfs(cn):
    flg=0
    if visited[cn]==0:
        flg=1
        L.append(cn)
    visited[cn]=1
    for i in range(n):
        if cn!=i and G[cn][i]==1 and visited[i]==0:
            dfs(i)
    if flg==1:
        L.append(cn) dfs flg=0 flg 0 if visited[cn]==0:
        flg=1
        L.append(cn)
     visited[cn]==0 visited[cn] visited cn 0 flg=1 flg 1 L.append(cn) L.append L append cn visited[cn]=1 visited[cn] visited cn 1 for i in range(n):
        if cn!=i and G[cn][i]==1 and visited[i]==0:
            dfs(i)
     i range(n) range n if cn!=i and G[cn][i]==1 and visited[i]==0:
            dfs(i)
     cn!=i and G[cn][i]==1 and visited[i]==0 cn!=i and G[cn][i]==1 cn!=i cn i G[cn][i]==1 G[cn][i] [cn] G cn i 1 visited[i]==0 visited[i] visited i 0 dfs(i) dfs i if flg==1:
        L.append(cn) flg==1 flg 1 L.append(cn) L.append L append cn cn cn i=0 i 0 while visited.count(1)<n:
    dfs(i)
    i+=1 visited.count(1)<n visited.count(1) visited.count visited count 1 n dfs(i) dfs i i+=1 i 1 0 for i in range(len(L)) for i in range(len(L)) i range(len(L)) range len(L) len L for i in range(len(L)) 0 copied=[0 for i in range(len(L))] copied [0 for i in range(len(L))] for i in range(len(L)):
    if copied[L[i]]==0:
        copied[L[i]]=1
        out[L[i]][1]=i+1
    elif copied[L[i]]==1:
        copied[L[i]]=2
        out[L[i]][2]=i+1 i range(len(L)) range len(L) len L if copied[L[i]]==0:
        copied[L[i]]=1
        out[L[i]][1]=i+1
    elif copied[L[i]]==1:
        copied[L[i]]=2
        out[L[i]][2]=i+1 copied[L[i]]==0 copied[L[i]] copied L[i] L i 0 copied[L[i]]=1 copied[L[i]] copied L[i] L i 1 out[L[i]][1]=i+1 out[L[i]][1] [L[i]] out L[i] L i 1 i+1 i 1 elif copied[L[i]]==1:
        copied[L[i]]=2
        out[L[i]][2]=i+1 copied[L[i]]==1 copied[L[i]] copied L[i] L i 1 copied[L[i]]=2 copied[L[i]] copied L[i] L i 2 out[L[i]][2]=i+1 out[L[i]][2] [L[i]] out L[i] L i 2 i+1 i 1 ' '.join(map(str,i)) for i in out for i in out i out for i in out ' '.join(map(str,i)) ' '.join ' ' join map(str,i) map str i out=[' '.join(map(str,i)) for i in out] out [' '.join(map(str,i)) for i in out] print('\n'.join(map(str,out))) print '\n'.join(map(str,out)) '\n'.join '\n' join map(str,out) map str out g_in=list(map(int,input().split())) list(map(int,input().split())) g_in L=[] [] L visited=[0 for i in range(n)] [0 for i in range(n)] visited i=0 0 i def dfs(cn):
    flg=0
    if visited[cn]==0:
        flg=1
        L.append(cn)
    visited[cn]=1
    for i in range(n):
        if cn!=i and G[cn][i]==1 and visited[i]==0:
            dfs(i)
    if flg==1:
        L.append(cn) def dfs(cn):
    flg=0
    if visited[cn]==0:
        flg=1
        L.append(cn)
    visited[cn]=1
    for i in range(n):
        if cn!=i and G[cn][i]==1 and visited[i]==0:
            dfs(i)
    if flg==1:
        L.append(cn) dfs v=g_in[2:] g_in[2:] v out=[[i+1,0,0] for i in range(n)] [[i+1,0,0] for i in range(n)] out n=int(input()) int(input()) n u=g_in[0] g_in[0] u i+=1 1 i copied=[0 for i in range(len(L))] [0 for i in range(len(L))] copied finished=[0 for i in range(n)] [0 for i in range(n)] finished step=0 0 step k=g_in[1] g_in[1] k G=[[j for j in range(n)] for i in range(n)] [[j for j in range(n)] for i in range(n)] G out=[' '.join(map(str,i)) for i in out] [' '.join(map(str,i)) for i in out] out
n=int(input())
G=[[j for j in range(n)] for i in range(n)]
for i in range(n):
    g_in=list(map(int,input().split()))
    u=g_in[0]
    k=g_in[1]
    v=g_in[2:]
    for j in v:
        G[u-1][j-1]=1

visited=[0 for i in range(n)]
finished=[0 for i in range(n)]
L=[]
out=[[i+1,0,0] for i in range(n)]
step=0

def dfs(cn):
    flg=0
    if visited[cn]==0:
        flg=1
        L.append(cn)
    visited[cn]=1
    for i in range(n):
        if cn!=i and G[cn][i]==1 and visited[i]==0:
            dfs(i)
    if flg==1:
        L.append(cn)

i=0
while visited.count(1)<n:
    dfs(i)
    i+=1
copied=[0 for i in range(len(L))]

for i in range(len(L)):
    if copied[L[i]]==0:
        copied[L[i]]=1
        out[L[i]][1]=i+1
    elif copied[L[i]]==1:
        copied[L[i]]=2
        out[L[i]][2]=i+1

out=[' '.join(map(str,i)) for i in out]
#print(' '.join(map(str,[i+1 for i in L])))
print('\n'.join(map(str,out)))
