#16D8101014F KurumeRyunosuke 2018/6/20

n=int(input())
ls=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
	tmp=input().split()
	for j in range(int(tmp[1])):
		ls[i][int(tmp[j+2])-1]=1

"""
for i in range(n):
    for j in range(n):
        if(j!=n-1):
            print(int(ls[i][j]), end=' ')
        else:
            print(int(ls[i][j]))
"""
stuck=[0]
count=1
d=[0 for i in range(n)]
f=[0 for i in range(n)]
d[0]=count
for i in range(n):
	ls[i][0]=0

while True:
	count+=1
	p=0
	#print("S",d)
	for i in range(n):
		if ls[stuck[len(stuck)-1]][i] is not 0:
			ls[stuck[len(stuck)-1]][i]=0
			p=1
			#print(stuck)
			stuck.append(i)
			d[stuck[len(stuck)-1]]=count
			break
	#print(d,"**")
	if p is 1:
		for i in range(n):
			ls[i][stuck[len(stuck)-1]]=0

	if p is 0:
		#print("//",count)
		a=stuck.pop()
		f[a]=count
		for i in range(n):
			ls[a][i]=0
	#print(d,"¥¥¥")
	if len(stuck) is 0:
		t=0
		for i in range(n):
			for j in range(n):
				if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		if t is 0:
			break
		#print("break")
	#print("G",d,f,count)

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)] ls=[[0 for i in range(n)]for j in range(n)] ls [[0 for i in range(n)]for j in range(n)] for i in range(n):
	tmp=input().split()
	for j in range(int(tmp[1])):
		ls[i][int(tmp[j+2])-1]=1 i range(n) range n tmp=input().split() tmp input().split() ().split () input split for j in range(int(tmp[1])):
		ls[i][int(tmp[j+2])-1]=1 j range(int(tmp[1])) range int(tmp[1]) int tmp[1] tmp 1 ls[i][int(tmp[j+2])-1]=1 ls[i][int(tmp[j+2])-1] [i] ls i int(tmp[j+2])-1 int(tmp[j+2]) int tmp[j+2] tmp j+2 j 2 1 1 """
for i in range(n):
    for j in range(n):
        if(j!=n-1):
            print(int(ls[i][j]), end=' ')
        else:
            print(int(ls[i][j]))
""" stuck=[0] stuck [0] 0 count=1 count 1 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 d=[0 for i in range(n)] d [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 f=[0 for i in range(n)] f [0 for i in range(n)] d[0]=count d[0] d 0 count for i in range(n):
	ls[i][0]=0 i range(n) range n ls[i][0]=0 ls[i][0] [i] ls i 0 0 while True:
	count+=1
	p=0
	#print("S",d)
	for i in range(n):
		if ls[stuck[len(stuck)-1]][i] is not 0:
			ls[stuck[len(stuck)-1]][i]=0
			p=1
			#print(stuck)
			stuck.append(i)
			d[stuck[len(stuck)-1]]=count
			break
	#print(d,"**")
	if p is 1:
		for i in range(n):
			ls[i][stuck[len(stuck)-1]]=0

	if p is 0:
		#print("//",count)
		a=stuck.pop()
		f[a]=count
		for i in range(n):
			ls[a][i]=0
	#print(d,"¥¥¥")
	if len(stuck) is 0:
		t=0
		for i in range(n):
			for j in range(n):
				if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		if t is 0:
			break
		#print("break")
	#print("G",d,f,count) True count+=1 count 1 p=0 p 0 for i in range(n):
		if ls[stuck[len(stuck)-1]][i] is not 0:
			ls[stuck[len(stuck)-1]][i]=0
			p=1
			#print(stuck)
			stuck.append(i)
			d[stuck[len(stuck)-1]]=count
			break
	#print(d,"**")
	 i range(n) range n if ls[stuck[len(stuck)-1]][i] is not 0:
			ls[stuck[len(stuck)-1]][i]=0
			p=1
			#print(stuck)
			stuck.append(i)
			d[stuck[len(stuck)-1]]=count
			break
	#print(d,"**")
	 ls[stuck[len(stuck)-1]][i] is not 0 ls[stuck[len(stuck)-1]][i] [stuck[len(stuck)-1]] ls stuck[len(stuck)-1] stuck len(stuck)-1 len(stuck) len stuck 1 i 0 ls[stuck[len(stuck)-1]][i]=0 ls[stuck[len(stuck)-1]][i] [stuck[len(stuck)-1]] ls stuck[len(stuck)-1] stuck len(stuck)-1 len(stuck) len stuck 1 i 0 p=1 p 1 stuck.append(i) stuck.append stuck append i d[stuck[len(stuck)-1]]=count d[stuck[len(stuck)-1]] d stuck[len(stuck)-1] stuck len(stuck)-1 len(stuck) len stuck 1 count break if p is 1:
		for i in range(n):
			ls[i][stuck[len(stuck)-1]]=0

	 p is 1 p 1 for i in range(n):
			ls[i][stuck[len(stuck)-1]]=0

	 i range(n) range n ls[i][stuck[len(stuck)-1]]=0 ls[i][stuck[len(stuck)-1]] [i] ls i stuck[len(stuck)-1] stuck len(stuck)-1 len(stuck) len stuck 1 0 if p is 0:
		#print("//",count)
		a=stuck.pop()
		f[a]=count
		for i in range(n):
			ls[a][i]=0
	#print(d,"¥¥¥")
	 p is 0 p 0 a=stuck.pop() a stuck.pop() stuck.pop stuck pop f[a]=count f[a] f a count for i in range(n):
			ls[a][i]=0
	#print(d,"¥¥¥")
	 i range(n) range n ls[a][i]=0 ls[a][i] [a] ls a i 0 if len(stuck) is 0:
		t=0
		for i in range(n):
			for j in range(n):
				if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		if t is 0:
			break
		#print("break")
	#print("G",d,f,count) len(stuck) is 0 len(stuck) len stuck 0 t=0 t 0 for i in range(n):
			for j in range(n):
				if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		 i range(n) range n for j in range(n):
				if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		 j range(n) range n if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		 ls[i][j] is not 0 and t is 0 ls[i][j] is not 0 ls[i][j] [i] ls i j 0 t is 0 t 0 count+=1 count 1 stuck.append(i) stuck.append stuck append i d[stuck[len(stuck)-1]]=count d[stuck[len(stuck)-1]] d stuck[len(stuck)-1] stuck len(stuck)-1 len(stuck) len stuck 1 count t=1 t 1 if t is 0:
			break
		#print("break")
	#print("G",d,f,count) t is 0 t 0 break 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 count+=1 1 count d=[0 for i in range(n)] [0 for i in range(n)] d n=int(input()) int(input()) n t=0 0 t tmp=input().split() input().split() tmp ls=[[0 for i in range(n)]for j in range(n)] [[0 for i in range(n)]for j in range(n)] ls stuck=[0] [0] stuck count=1 1 count p=1 1 p count+=1 1 count t=1 1 t f=[0 for i in range(n)] [0 for i in range(n)] f p=0 0 p a=stuck.pop() stuck.pop() a
#16D8101014F KurumeRyunosuke 2018/6/20

n=int(input())
ls=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
	tmp=input().split()
	for j in range(int(tmp[1])):
		ls[i][int(tmp[j+2])-1]=1

"""
for i in range(n):
    for j in range(n):
        if(j!=n-1):
            print(int(ls[i][j]), end=' ')
        else:
            print(int(ls[i][j]))
"""
stuck=[0]
count=1
d=[0 for i in range(n)]
f=[0 for i in range(n)]
d[0]=count
for i in range(n):
	ls[i][0]=0

while True:
	count+=1
	p=0
	#print("S",d)
	for i in range(n):
		if ls[stuck[len(stuck)-1]][i] is not 0:
			ls[stuck[len(stuck)-1]][i]=0
			p=1
			#print(stuck)
			stuck.append(i)
			d[stuck[len(stuck)-1]]=count
			break
	#print(d,"**")
	if p is 1:
		for i in range(n):
			ls[i][stuck[len(stuck)-1]]=0

	if p is 0:
		#print("//",count)
		a=stuck.pop()
		f[a]=count
		for i in range(n):
			ls[a][i]=0
	#print(d,"¥¥¥")
	if len(stuck) is 0:
		t=0
		for i in range(n):
			for j in range(n):
				if ls[i][j] is not 0 and t is 0:
					count+=1
					stuck.append(i)
					d[stuck[len(stuck)-1]]=count
					#print(i,"***")
					t=1
					
		if t is 0:
			break
		#print("break")
	#print("G",d,f,count)

for i in range(n):
	print(i+1,d[i],f[i])

