def readinput():
    n=int(input())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for _ in range(n):
        l=list(map(int,input().split()))
        nList[l[0]]=l[2:]
    return n,nList

WHITE=0
GRAY=1
BLACK=2
color=[]
d=[]
f=[]
_time=0

def dfs(u,nList):
    global color
    global d
    global f
    global _time

    S=[]

    if(color[u]==WHITE):
        S.append(u)
        color[u]=GRAY
        _time+=1
        d[u]=_time

    while(len(S)>0):
        u=S[-1]
        t=next(u,nList)
        if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time

def next(u,nList):
    global color
    for t in nList[u]:
        if color[t]==WHITE:
            return t
    else:
        return -1

def main(n,nList):
    global color
    global d
    global f

    color=[WHITE]*(n+1)
    d=[0]*(n+1)
    f=[0]*(n+1)

    for i in range(1,n+1):
        dfs(i,nList)
    for i in range(1,n+1):
        print('{} {} {}'.format(i,d[i],f[i]))

if __name__=='__main__':
    n,nList=readinput()
    main(n,nList)
 def readinput():
    n=int(input())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for _ in range(n):
        l=list(map(int,input().split()))
        nList[l[0]]=l[2:]
    return n,nList readinput n=int(input()) n int(input()) int input() input nList=[] nList [] for _ in range(n+1):
        nList.append([])
     _ range(n+1) range n+1 n 1 nList.append([]) nList.append nList append [] for _ in range(n):
        l=list(map(int,input().split()))
        nList[l[0]]=l[2:]
     _ range(n) range n l=list(map(int,input().split())) l list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split nList[l[0]]=l[2:] nList[l[0]] nList l[0] l 0 l[2:] l 2: 2 return n,nList return n,nList n nList WHITE=0 WHITE 0 GRAY=1 GRAY 1 BLACK=2 BLACK 2 color=[] color [] d=[] d [] f=[] f [] _time=0 _time 0 def dfs(u,nList):
    global color
    global d
    global f
    global _time

    S=[]

    if(color[u]==WHITE):
        S.append(u)
        color[u]=GRAY
        _time+=1
        d[u]=_time

    while(len(S)>0):
        u=S[-1]
        t=next(u,nList)
        if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time dfs global color global d global f global _time S=[] S [] if(color[u]==WHITE):
        S.append(u)
        color[u]=GRAY
        _time+=1
        d[u]=_time

     color[u]==WHITE color[u] color u WHITE S.append(u) S.append S append u color[u]=GRAY color[u] color u GRAY _time+=1 _time 1 d[u]=_time d[u] d u _time while(len(S)>0):
        u=S[-1]
        t=next(u,nList)
        if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time len(S)>0 len(S) len S 0 u=S[-1] u S[-1] S -1 t=next(u,nList) t next(u,nList) next u nList if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time t!=-1 t -1 if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
         color[t]==WHITE color[t] color t WHITE S.append(t) S.append S append t color[t]=GRAY color[t] color t GRAY _time+=1 _time 1 d[t]=_time d[t] d t _time S.pop() S.pop S pop color[u]=BLACK color[u] color u BLACK _time+=1 _time 1 f[u]=_time f[u] f u _time u u nList nList def next(u,nList):
    global color
    for t in nList[u]:
        if color[t]==WHITE:
            return t
    else:
        return -1 next global color for t in nList[u]:
        if color[t]==WHITE:
            return t
    else:
        return -1 t nList[u] nList u if color[t]==WHITE:
            return t
     color[t]==WHITE color[t] color t WHITE return t t return -1 -1 u u nList nList def main(n,nList):
    global color
    global d
    global f

    color=[WHITE]*(n+1)
    d=[0]*(n+1)
    f=[0]*(n+1)

    for i in range(1,n+1):
        dfs(i,nList)
    for i in range(1,n+1):
        print('{} {} {}'.format(i,d[i],f[i])) main global color global d global f color=[WHITE]*(n+1) color [WHITE]*(n+1) [WHITE] WHITE n+1 n 1 d=[0]*(n+1) d [0]*(n+1) [0] 0 n+1 n 1 f=[0]*(n+1) f [0]*(n+1) [0] 0 n+1 n 1 for i in range(1,n+1):
        dfs(i,nList)
     i range(1,n+1) range 1 n+1 n 1 dfs(i,nList) dfs i nList for i in range(1,n+1):
        print('{} {} {}'.format(i,d[i],f[i])) i range(1,n+1) range 1 n+1 n 1 print('{} {} {}'.format(i,d[i],f[i])) print '{} {} {}'.format(i,d[i],f[i]) '{} {} {}'.format '{} {} {}' format i d[i] d i f[i] f i n n nList nList if __name__=='__main__':
    n,nList=readinput()
    main(n,nList) __name__=='__main__' __name__ '__main__' n,nList=readinput() n readinput() readinput nList main(n,nList) main n nList BLACK=2 2 BLACK d=[] [] d def next(u,nList):
    global color
    for t in nList[u]:
        if color[t]==WHITE:
            return t
    else:
        return -1 def next(u,nList):
    global color
    for t in nList[u]:
        if color[t]==WHITE:
            return t
    else:
        return -1 next def main(n,nList):
    global color
    global d
    global f

    color=[WHITE]*(n+1)
    d=[0]*(n+1)
    f=[0]*(n+1)

    for i in range(1,n+1):
        dfs(i,nList)
    for i in range(1,n+1):
        print('{} {} {}'.format(i,d[i],f[i])) def main(n,nList):
    global color
    global d
    global f

    color=[WHITE]*(n+1)
    d=[0]*(n+1)
    f=[0]*(n+1)

    for i in range(1,n+1):
        dfs(i,nList)
    for i in range(1,n+1):
        print('{} {} {}'.format(i,d[i],f[i])) main def readinput():
    n=int(input())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for _ in range(n):
        l=list(map(int,input().split()))
        nList[l[0]]=l[2:]
    return n,nList def readinput():
    n=int(input())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for _ in range(n):
        l=list(map(int,input().split()))
        nList[l[0]]=l[2:]
    return n,nList readinput WHITE=0 0 WHITE nList=readinput() readinput() nList color=[] [] color f=[] [] f _time=0 0 _time n,nList=readinput() readinput() n def dfs(u,nList):
    global color
    global d
    global f
    global _time

    S=[]

    if(color[u]==WHITE):
        S.append(u)
        color[u]=GRAY
        _time+=1
        d[u]=_time

    while(len(S)>0):
        u=S[-1]
        t=next(u,nList)
        if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time def dfs(u,nList):
    global color
    global d
    global f
    global _time

    S=[]

    if(color[u]==WHITE):
        S.append(u)
        color[u]=GRAY
        _time+=1
        d[u]=_time

    while(len(S)>0):
        u=S[-1]
        t=next(u,nList)
        if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time dfs GRAY=1 1 GRAY
def readinput():
    n=int(input())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for _ in range(n):
        l=list(map(int,input().split()))
        nList[l[0]]=l[2:]
    return n,nList

WHITE=0
GRAY=1
BLACK=2
color=[]
d=[]
f=[]
_time=0

def dfs(u,nList):
    global color
    global d
    global f
    global _time

    S=[]

    if(color[u]==WHITE):
        S.append(u)
        color[u]=GRAY
        _time+=1
        d[u]=_time

    while(len(S)>0):
        u=S[-1]
        t=next(u,nList)
        if(t!=-1):
            if(color[t]==WHITE):
                S.append(t)
                color[t]=GRAY
                _time+=1
                d[t]=_time
        else:
            S.pop()
            color[u]=BLACK
            _time+=1
            f[u]=_time

def next(u,nList):
    global color
    for t in nList[u]:
        if color[t]==WHITE:
            return t
    else:
        return -1

def main(n,nList):
    global color
    global d
    global f

    color=[WHITE]*(n+1)
    d=[0]*(n+1)
    f=[0]*(n+1)

    for i in range(1,n+1):
        dfs(i,nList)
    for i in range(1,n+1):
        print('{} {} {}'.format(i,d[i],f[i]))

if __name__=='__main__':
    n,nList=readinput()
    main(n,nList)

