import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect


# from operator import mul

# sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = getSomeInputListInt(n)

ans = [[] for _ in range(n)]

edge = []
for d in data:
    edge.append(d[2:])


def dfs(i, cnt):
    if len(ans[i]) > 0:
        return cnt

    cnt += 1

    ans[i].append(cnt)
    for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

    cnt += 1
    ans[i].append(cnt)

    return cnt

tmp = 0
for i in range(n):
    tmp = dfs(i, tmp)

for i in range(n):
    print(i + 1, *ans[i], sep=" ")
 import math math import copy copy import sys sys import fractions fractions import decimal decimal import heapq heapq import collections collections import itertools itertools import bisect bisect def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList getInputIntList outputDataList = [] outputDataList [] inputData = input().split() inputData input().split() ().split () input split int(n) for n in inputData for n in inputData n inputData for n in inputData int(n) int n outputDataList = [int(n) for n in inputData] outputDataList [int(n) for n in inputData] return outputDataList outputDataList def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList getSomeInputInt outputDataList = [] outputDataList [] for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

     i range(n) range n inputData = int(input()) inputData int(input()) int input() input outputDataList.append(inputData) outputDataList.append outputDataList append inputData return outputDataList outputDataList n n def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE=== getSomeInputListInt inputDataList = [] inputDataList [] outputDataList = [] outputDataList [] for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

     i range(n) range n inputData = input().split() inputData input().split() ().split () input split int(n) - 1 for n in inputData for n in inputData n inputData for n in inputData int(n) - 1 int(n) int n 1 inputDataList = [int(n) - 1 for n in inputData] inputDataList [int(n) - 1 for n in inputData] outputDataList.append(inputDataList) outputDataList.append outputDataList append inputDataList return outputDataList outputDataList n n n = int(input()) n int(input()) int input() input data = getSomeInputListInt(n) data getSomeInputListInt(n) getSomeInputListInt n [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] ans = [[] for _ in range(n)] ans [[] for _ in range(n)] edge = [] edge [] for d in data:
    edge.append(d[2:]) d data edge.append(d[2:]) edge.append edge append d[2:] d 2: 2 def dfs(i, cnt):
    if len(ans[i]) > 0:
        return cnt

    cnt += 1

    ans[i].append(cnt)
    for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

    cnt += 1
    ans[i].append(cnt)

    return cnt dfs if len(ans[i]) > 0:
        return cnt

     len(ans[i]) > 0 len(ans[i]) len ans[i] ans i 0 return cnt cnt cnt += 1 cnt 1 ans[i].append(cnt) [i].append [i] ans i append cnt for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

     j range(len(edge[i])) range len(edge[i]) len edge[i] edge i cnt = dfs(edge[i][j], cnt) cnt dfs(edge[i][j], cnt) dfs edge[i][j] [i] edge i j cnt cnt += 1 cnt 1 ans[i].append(cnt) [i].append [i] ans i append cnt return cnt cnt i i cnt cnt tmp = 0 tmp 0 for i in range(n):
    tmp = dfs(i, tmp) i range(n) range n tmp = dfs(i, tmp) tmp dfs(i, tmp) dfs i tmp for i in range(n):
    print(i + 1, *ans[i], sep=" ") i range(n) range n print(i + 1, *ans[i], sep=" ") print i + 1 i 1 *ans[i] ans[i] ans i sep=" " sep " " tmp = 0 0 tmp def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList getSomeInputInt tmp = dfs(i, tmp) dfs(i, tmp) tmp def dfs(i, cnt):
    if len(ans[i]) > 0:
        return cnt

    cnt += 1

    ans[i].append(cnt)
    for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

    cnt += 1
    ans[i].append(cnt)

    return cnt def dfs(i, cnt):
    if len(ans[i]) > 0:
        return cnt

    cnt += 1

    ans[i].append(cnt)
    for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

    cnt += 1
    ans[i].append(cnt)

    return cnt dfs def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList getInputIntList def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE=== def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE=== getSomeInputListInt n = int(input()) int(input()) n ans = [[] for _ in range(n)] [[] for _ in range(n)] ans data = getSomeInputListInt(n) getSomeInputListInt(n) data edge = [] [] edge
import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect


# from operator import mul

# sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) - 1 for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = getSomeInputListInt(n)

ans = [[] for _ in range(n)]

edge = []
for d in data:
    edge.append(d[2:])


def dfs(i, cnt):
    if len(ans[i]) > 0:
        return cnt

    cnt += 1

    ans[i].append(cnt)
    for j in range(len(edge[i])):
        cnt = dfs(edge[i][j], cnt)

    cnt += 1
    ans[i].append(cnt)

    return cnt

tmp = 0
for i in range(n):
    tmp = dfs(i, tmp)

for i in range(n):
    print(i + 1, *ans[i], sep=" ")

