# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B&lang=ja
import sys
input = sys.stdin.readline

N = int(input())
G = [[a-1 for a in list(map(int, input().split()))[2:]] for _ in [0]*N]
visited = [0]*N
history = [[] for _ in [0]*N]

k = 0


def dfs(v=0, p=-1):
    global k
    visited[v] = 1
    k += 1
    history[v].append(k)

    for u in G[v]:
        if u == p or visited[u]:
            continue
        dfs(u, v)

    k += 1
    history[v].append(k)


for i in range(N):
    if not visited[i]:
        dfs(i)

for i, h in enumerate(history):
    print(i+1, *h)
 import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N = int(input()) N int(input()) int input() input [a-1 for a in list(map(int, input().split()))[2:]] for _ in [0]*N for _ in [0]*N _ [0]*N [0] 0 N for _ in [0]*N [a-1 for a in list(map(int, input().split()))[2:]] G = [[a-1 for a in list(map(int, input().split()))[2:]] for _ in [0]*N] G [[a-1 for a in list(map(int, input().split()))[2:]] for _ in [0]*N] visited = [0]*N visited [0]*N [0] 0 N [] for _ in [0]*N for _ in [0]*N _ [0]*N [0] 0 N for _ in [0]*N [] history = [[] for _ in [0]*N] history [[] for _ in [0]*N] k = 0 k 0 def dfs(v=0, p=-1):
    global k
    visited[v] = 1
    k += 1
    history[v].append(k)

    for u in G[v]:
        if u == p or visited[u]:
            continue
        dfs(u, v)

    k += 1
    history[v].append(k) dfs global k visited[v] = 1 visited[v] visited v 1 k += 1 k 1 history[v].append(k) [v].append [v] history v append k for u in G[v]:
        if u == p or visited[u]:
            continue
        dfs(u, v)

     u G[v] G v if u == p or visited[u]:
            continue
         u == p or visited[u] u == p u p visited[u] visited u continue dfs(u, v) dfs u v k += 1 k 1 history[v].append(k) [v].append [v] history v append k v=0 v 0 p=-1 p -1 for i in range(N):
    if not visited[i]:
        dfs(i) i range(N) range N if not visited[i]:
        dfs(i) not visited[i] visited[i] visited i dfs(i) dfs i for i, h in enumerate(history):
    print(i+1, *h) i h enumerate(history) enumerate history print(i+1, *h) print i+1 i 1 *h h N = int(input()) int(input()) N history = [[] for _ in [0]*N] [[] for _ in [0]*N] history k = 0 0 k input = sys.stdin.readline sys.stdin.readline input visited = [0]*N [0]*N visited G = [[a-1 for a in list(map(int, input().split()))[2:]] for _ in [0]*N] [[a-1 for a in list(map(int, input().split()))[2:]] for _ in [0]*N] G def dfs(v=0, p=-1):
    global k
    visited[v] = 1
    k += 1
    history[v].append(k)

    for u in G[v]:
        if u == p or visited[u]:
            continue
        dfs(u, v)

    k += 1
    history[v].append(k) def dfs(v=0, p=-1):
    global k
    visited[v] = 1
    k += 1
    history[v].append(k)

    for u in G[v]:
        if u == p or visited[u]:
            continue
        dfs(u, v)

    k += 1
    history[v].append(k) dfs
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B&lang=ja
import sys
input = sys.stdin.readline

N = int(input())
G = [[a-1 for a in list(map(int, input().split()))[2:]] for _ in [0]*N]
visited = [0]*N
history = [[] for _ in [0]*N]

k = 0


def dfs(v=0, p=-1):
    global k
    visited[v] = 1
    k += 1
    history[v].append(k)

    for u in G[v]:
        if u == p or visited[u]:
            continue
        dfs(u, v)

    k += 1
    history[v].append(k)


for i in range(N):
    if not visited[i]:
        dfs(i)

for i, h in enumerate(history):
    print(i+1, *h)

