#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2


def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix


def graph_dfs(v_init):
    global time

    # init the first node of overall graph iterations(times >= 1)
    vertices_visit_stack.append(v_init)
    vertices_status_list[v_init] = VISITED_IN_STACK
    vertices_d_time[v_init] += time
    # init end

    while len(vertices_visit_stack) > 1:
        current_vertex = vertices_visit_stack[-1]
        next_vertex = get_adj_vertices(vertex=current_vertex)

        if next_vertex:
            if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
        else:
            vertices_visit_stack.pop()
            vertices_status_list[current_vertex] = POPPED_OUT
            time += 1
            vertices_f_time[current_vertex] += time

    return None


def get_adj_vertices(vertex):
    index = int(vertex) - 1
    for j in range(vertices_num):
        if adj_matrix[index][j]:
            adj_matrix[index][j] = 0
            return j + 1
    return None


def dfs_start():
    global time
    for i in range(vertices_num):
        if vertices_status_list[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(v_init=i + 1)
            time += 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    vertices_info = list(map(lambda x: x.split(), _input[1:]))
    init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    vertices_visit_stack = [-1]
    vertices_status_list = [UNVISITED] * vertices_num
    vertices_status_list.insert(0, -1)
    vertices_d_time, vertices_f_time = ([0] * (vertices_num + 1) for _ in range(2))

    adj_matrix = generate_adj_matrix(vertices_info)
    # timing start from 1
    time = 1
    dfs_start()
    for k in range(vertices_num):
        print(k + 1, vertices_d_time[k + 1], vertices_f_time[k + 1]) import sys sys UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2 UNVISITED 0 VISITED_IN_STACK 1 POPPED_OUT 2 def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix generate_adj_matrix for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
     v_detail v_info v_adj_list = v_detail[2:] v_adj_list v_detail[2:] v_detail 2: 2 for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
     each v_adj_list init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1 init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] [int(v_detail[0]) - 1] init_adj_matrix int(v_detail[0]) - 1 int(v_detail[0]) int v_detail[0] v_detail 0 1 int(each) - 1 int(each) int each 1 1 return init_adj_matrix init_adj_matrix v_info v_info def graph_dfs(v_init):
    global time

    # init the first node of overall graph iterations(times >= 1)
    vertices_visit_stack.append(v_init)
    vertices_status_list[v_init] = VISITED_IN_STACK
    vertices_d_time[v_init] += time
    # init end

    while len(vertices_visit_stack) > 1:
        current_vertex = vertices_visit_stack[-1]
        next_vertex = get_adj_vertices(vertex=current_vertex)

        if next_vertex:
            if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
        else:
            vertices_visit_stack.pop()
            vertices_status_list[current_vertex] = POPPED_OUT
            time += 1
            vertices_f_time[current_vertex] += time

    return None graph_dfs global time vertices_visit_stack.append(v_init) vertices_visit_stack.append vertices_visit_stack append v_init vertices_status_list[v_init] = VISITED_IN_STACK vertices_status_list[v_init] vertices_status_list v_init VISITED_IN_STACK vertices_d_time[v_init] += time vertices_d_time[v_init] vertices_d_time v_init time while len(vertices_visit_stack) > 1:
        current_vertex = vertices_visit_stack[-1]
        next_vertex = get_adj_vertices(vertex=current_vertex)

        if next_vertex:
            if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
        else:
            vertices_visit_stack.pop()
            vertices_status_list[current_vertex] = POPPED_OUT
            time += 1
            vertices_f_time[current_vertex] += time

     len(vertices_visit_stack) > 1 len(vertices_visit_stack) len vertices_visit_stack 1 current_vertex = vertices_visit_stack[-1] current_vertex vertices_visit_stack[-1] vertices_visit_stack -1 next_vertex = get_adj_vertices(vertex=current_vertex) next_vertex get_adj_vertices(vertex=current_vertex) get_adj_vertices vertex=current_vertex vertex current_vertex if next_vertex:
            if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
        else:
            vertices_visit_stack.pop()
            vertices_status_list[current_vertex] = POPPED_OUT
            time += 1
            vertices_f_time[current_vertex] += time

     next_vertex if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
         vertices_status_list[next_vertex] is UNVISITED vertices_status_list[next_vertex] vertices_status_list next_vertex UNVISITED vertices_status_list[next_vertex] = VISITED_IN_STACK vertices_status_list[next_vertex] vertices_status_list next_vertex VISITED_IN_STACK time += 1 time 1 vertices_d_time[next_vertex] += time vertices_d_time[next_vertex] vertices_d_time next_vertex time vertices_visit_stack.append(next_vertex) vertices_visit_stack.append vertices_visit_stack append next_vertex vertices_visit_stack.pop() vertices_visit_stack.pop vertices_visit_stack pop vertices_status_list[current_vertex] = POPPED_OUT vertices_status_list[current_vertex] vertices_status_list current_vertex POPPED_OUT time += 1 time 1 vertices_f_time[current_vertex] += time vertices_f_time[current_vertex] vertices_f_time current_vertex time return None None v_init v_init def get_adj_vertices(vertex):
    index = int(vertex) - 1
    for j in range(vertices_num):
        if adj_matrix[index][j]:
            adj_matrix[index][j] = 0
            return j + 1
    return None get_adj_vertices index = int(vertex) - 1 index int(vertex) - 1 int(vertex) int vertex 1 for j in range(vertices_num):
        if adj_matrix[index][j]:
            adj_matrix[index][j] = 0
            return j + 1
     j range(vertices_num) range vertices_num if adj_matrix[index][j]:
            adj_matrix[index][j] = 0
            return j + 1
     adj_matrix[index][j] [index] adj_matrix index j adj_matrix[index][j] = 0 adj_matrix[index][j] [index] adj_matrix index j 0 return j + 1 j + 1 j 1 return None None vertex vertex def dfs_start():
    global time
    for i in range(vertices_num):
        if vertices_status_list[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(v_init=i + 1)
            time += 1 dfs_start global time for i in range(vertices_num):
        if vertices_status_list[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(v_init=i + 1)
            time += 1 i range(vertices_num) range vertices_num if vertices_status_list[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(v_init=i + 1)
            time += 1 vertices_status_list[i + 1] == UNVISITED vertices_status_list[i + 1] vertices_status_list i + 1 i 1 UNVISITED graph_dfs(v_init=i + 1) graph_dfs v_init=i + 1 v_init i + 1 i 1 time += 1 time 1 if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    vertices_info = list(map(lambda x: x.split(), _input[1:]))
    init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    vertices_visit_stack = [-1]
    vertices_status_list = [UNVISITED] * vertices_num
    vertices_status_list.insert(0, -1)
    vertices_d_time, vertices_f_time = ([0] * (vertices_num + 1) for _ in range(2))

    adj_matrix = generate_adj_matrix(vertices_info)
    # timing start from 1
    time = 1
    dfs_start()
    for k in range(vertices_num):
        print(k + 1, vertices_d_time[k + 1], vertices_f_time[k + 1]) __name__ == '__main__' __name__ '__main__' _input = sys.stdin.readlines() _input sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines vertices_num = int(_input[0]) vertices_num int(_input[0]) int _input[0] _input 0 vertices_info = list(map(lambda x: x.split(), _input[1:])) vertices_info list(map(lambda x: x.split(), _input[1:])) list map(lambda x: x.split(), _input[1:]) map lambda x: x.split() x.split() x.split x split x _input[1:] _input 1: 1 [0] * vertices_num for _ in range(vertices_num) for _ in range(vertices_num) _ range(vertices_num) range vertices_num for _ in range(vertices_num) [0] * vertices_num [0] 0 vertices_num init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)] init_adj_matrix [[0] * vertices_num for _ in range(vertices_num)] vertices_visit_stack = [-1] vertices_visit_stack [-1] -1 vertices_status_list = [UNVISITED] * vertices_num vertices_status_list [UNVISITED] * vertices_num [UNVISITED] UNVISITED vertices_num vertices_status_list.insert(0, -1) vertices_status_list.insert vertices_status_list insert 0 -1 [0] * (vertices_num + 1) for _ in range(2) for _ in range(2) _ range(2) range 2 for _ in range(2) [0] * (vertices_num + 1) [0] 0 vertices_num + 1 vertices_num 1 vertices_d_time, vertices_f_time = ([0] * (vertices_num + 1) for _ in range(2)) vertices_d_time ([0] * (vertices_num + 1) for _ in range(2)) vertices_f_time adj_matrix = generate_adj_matrix(vertices_info) adj_matrix generate_adj_matrix(vertices_info) generate_adj_matrix vertices_info time = 1 time 1 dfs_start() dfs_start for k in range(vertices_num):
        print(k + 1, vertices_d_time[k + 1], vertices_f_time[k + 1]) k range(vertices_num) range vertices_num print(k + 1, vertices_d_time[k + 1], vertices_f_time[k + 1]) print k + 1 k 1 vertices_d_time[k + 1] vertices_d_time k + 1 k 1 vertices_f_time[k + 1] vertices_f_time k + 1 k 1 def graph_dfs(v_init):
    global time

    # init the first node of overall graph iterations(times >= 1)
    vertices_visit_stack.append(v_init)
    vertices_status_list[v_init] = VISITED_IN_STACK
    vertices_d_time[v_init] += time
    # init end

    while len(vertices_visit_stack) > 1:
        current_vertex = vertices_visit_stack[-1]
        next_vertex = get_adj_vertices(vertex=current_vertex)

        if next_vertex:
            if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
        else:
            vertices_visit_stack.pop()
            vertices_status_list[current_vertex] = POPPED_OUT
            time += 1
            vertices_f_time[current_vertex] += time

    return None def graph_dfs(v_init):
    global time

    # init the first node of overall graph iterations(times >= 1)
    vertices_visit_stack.append(v_init)
    vertices_status_list[v_init] = VISITED_IN_STACK
    vertices_d_time[v_init] += time
    # init end

    while len(vertices_visit_stack) > 1:
        current_vertex = vertices_visit_stack[-1]
        next_vertex = get_adj_vertices(vertex=current_vertex)

        if next_vertex:
            if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
        else:
            vertices_visit_stack.pop()
            vertices_status_list[current_vertex] = POPPED_OUT
            time += 1
            vertices_f_time[current_vertex] += time

    return None graph_dfs POPPED_OUT = 0, 1, 2 2 POPPED_OUT vertices_num = int(_input[0]) int(_input[0]) vertices_num vertices_f_time = ([0] * (vertices_num + 1) for _ in range(2)) ([0] * (vertices_num + 1) for _ in range(2)) vertices_f_time def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix generate_adj_matrix vertices_info = list(map(lambda x: x.split(), _input[1:])) list(map(lambda x: x.split(), _input[1:])) vertices_info vertices_visit_stack = [-1] [-1] vertices_visit_stack _input = sys.stdin.readlines() sys.stdin.readlines() _input def dfs_start():
    global time
    for i in range(vertices_num):
        if vertices_status_list[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(v_init=i + 1)
            time += 1 def dfs_start():
    global time
    for i in range(vertices_num):
        if vertices_status_list[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(v_init=i + 1)
            time += 1 dfs_start def get_adj_vertices(vertex):
    index = int(vertex) - 1
    for j in range(vertices_num):
        if adj_matrix[index][j]:
            adj_matrix[index][j] = 0
            return j + 1
    return None def get_adj_vertices(vertex):
    index = int(vertex) - 1
    for j in range(vertices_num):
        if adj_matrix[index][j]:
            adj_matrix[index][j] = 0
            return j + 1
    return None get_adj_vertices VISITED_IN_STACK, POPPED_OUT = 0, 1, 2 1 VISITED_IN_STACK init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)] [[0] * vertices_num for _ in range(vertices_num)] init_adj_matrix vertices_d_time, vertices_f_time = ([0] * (vertices_num + 1) for _ in range(2)) ([0] * (vertices_num + 1) for _ in range(2)) vertices_d_time UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2 0 UNVISITED vertices_status_list = [UNVISITED] * vertices_num [UNVISITED] * vertices_num vertices_status_list time = 1 1 time adj_matrix = generate_adj_matrix(vertices_info) generate_adj_matrix(vertices_info) adj_matrix
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2


def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix


def graph_dfs(v_init):
    global time

    # init the first node of overall graph iterations(times >= 1)
    vertices_visit_stack.append(v_init)
    vertices_status_list[v_init] = VISITED_IN_STACK
    vertices_d_time[v_init] += time
    # init end

    while len(vertices_visit_stack) > 1:
        current_vertex = vertices_visit_stack[-1]
        next_vertex = get_adj_vertices(vertex=current_vertex)

        if next_vertex:
            if vertices_status_list[next_vertex] is UNVISITED:
                vertices_status_list[next_vertex] = VISITED_IN_STACK
                time += 1
                vertices_d_time[next_vertex] += time
                vertices_visit_stack.append(next_vertex)
        else:
            vertices_visit_stack.pop()
            vertices_status_list[current_vertex] = POPPED_OUT
            time += 1
            vertices_f_time[current_vertex] += time

    return None


def get_adj_vertices(vertex):
    index = int(vertex) - 1
    for j in range(vertices_num):
        if adj_matrix[index][j]:
            adj_matrix[index][j] = 0
            return j + 1
    return None


def dfs_start():
    global time
    for i in range(vertices_num):
        if vertices_status_list[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(v_init=i + 1)
            time += 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    vertices_info = list(map(lambda x: x.split(), _input[1:]))
    init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    vertices_visit_stack = [-1]
    vertices_status_list = [UNVISITED] * vertices_num
    vertices_status_list.insert(0, -1)
    vertices_d_time, vertices_f_time = ([0] * (vertices_num + 1) for _ in range(2))

    adj_matrix = generate_adj_matrix(vertices_info)
    # timing start from 1
    time = 1
    dfs_start()
    for k in range(vertices_num):
        print(k + 1, vertices_d_time[k + 1], vertices_f_time[k + 1])