import sys
input = lambda: sys.stdin.readline().rstrip()
n=int(input())
s=0 ; st=set() ; ans=[[] for i in range(n)] ; v=0
def dfs(g,v):
    global s,st,ans
    if v in st: return
    else: st.add(v)
    s+=1
    ans[v].append(s)
    for i in g[v]:
        dfs(g,i)
    s+=1
    ans[v].append(s)
g=[]
for _ in range(n):
    a=[int(i)-1 for i in input().split()]
    if a[1]==-1: g.append([])
    else: g.append(a[2:])
for i in range(n):
    if i in st : continue
    else : dfs(g,i)
for i in range(len(ans)):
    print(i+1,*ans[i])
    
    
     import sys sys input = lambda: sys.stdin.readline().rstrip() input lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip n=int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] s=0 s 0 st=set() st set() set ans=[[] for i in range(n)] ans [[] for i in range(n)] v=0 v 0 def dfs(g,v):
    global s,st,ans
    if v in st: return
    else: st.add(v)
    s+=1
    ans[v].append(s)
    for i in g[v]:
        dfs(g,i)
    s+=1
    ans[v].append(s) dfs global s,st,ans if v in st: return
    else: st.add(v)
     v in st v st s+=1 s 1 ans[v].append(s) [v].append [v] ans v append s for i in g[v]:
        dfs(g,i)
     i g[v] g v dfs(g,i) dfs g i s+=1 s 1 ans[v].append(s) [v].append [v] ans v append s g g v v g=[] g [] for _ in range(n):
    a=[int(i)-1 for i in input().split()]
    if a[1]==-1: g.append([])
    else: g.append(a[2:]) _ range(n) range n int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 a=[int(i)-1 for i in input().split()] a [int(i)-1 for i in input().split()] if a[1]==-1: g.append([])
    else: g.append(a[2:]) a[1]==-1 a[1] a 1 -1 for i in range(n):
    if i in st : continue
    else : dfs(g,i) i range(n) range n if i in st : continue
    else : dfs(g,i) i in st i st for i in range(len(ans)):
    print(i+1,*ans[i])
    
    
     i range(len(ans)) range len(ans) len ans print(i+1,*ans[i]) print i+1 i 1 *ans[i] ans[i] ans i ans=[[] for i in range(n)] [[] for i in range(n)] ans def dfs(g,v):
    global s,st,ans
    if v in st: return
    else: st.add(v)
    s+=1
    ans[v].append(s)
    for i in g[v]:
        dfs(g,i)
    s+=1
    ans[v].append(s) def dfs(g,v):
    global s,st,ans
    if v in st: return
    else: st.add(v)
    s+=1
    ans[v].append(s)
    for i in g[v]:
        dfs(g,i)
    s+=1
    ans[v].append(s) dfs st=set() set() st g=[] [] g n=int(input()) int(input()) n v=0 0 v input = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() input a=[int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] a s=0 0 s
import sys
input = lambda: sys.stdin.readline().rstrip()
n=int(input())
s=0 ; st=set() ; ans=[[] for i in range(n)] ; v=0
def dfs(g,v):
    global s,st,ans
    if v in st: return
    else: st.add(v)
    s+=1
    ans[v].append(s)
    for i in g[v]:
        dfs(g,i)
    s+=1
    ans[v].append(s)
g=[]
for _ in range(n):
    a=[int(i)-1 for i in input().split()]
    if a[1]==-1: g.append([])
    else: g.append(a[2:])
for i in range(n):
    if i in st : continue
    else : dfs(g,i)
for i in range(len(ans)):
    print(i+1,*ans[i])
    
    
    
