module Dfs
  # Node
  class Node
    attr_accessor :id, :edges, :visited, :progress

    def initialize(id, edges = [], visited = false, progress = false)
      @id, @edges, @visited, @progress = id, edges, visited, progress
    end
  end

  # Edge
  class Edge
    attr_accessor :to

    def initialize(to)
      @to = to
    end
  end

  # Graph
  class Graph
    attr_accessor :d, :f

    def initialize(data, counter)
      @counter = counter
      @d, @f = [], []
      @nodes = data.map do |id, edges|
        edges.map! { |edge| Edge.new(*edge) }
        Node.new(id, edges)
      end
    end

    def dfs(start)
      search(start)

      loop do
        visited_node = nil
        @nodes.each do |node|
          next if node.visited == true
          visited_node = node
          break
        end
        break if visited_node.nil?
        search(visited_node.id)
      end
    end

    def search(start)
      node = @nodes.find { |node| node.id == start }
      return if node.visited == true
      node.visited = true
      @d[node.id] = @counter.call
      node.edges.each do |edge|
        search edge.to
      end
      node.progress = true
      @f[node.id] = @counter.call
    end
  end
end

def create_counter
  count = 0
  return proc { count += 1 }
end

n = gets.chop.to_i

data = {}

n.times do
  u, k, *v = gets.chop.split.map(&:to_i)
  data[u] = v
end

g = Dfs::Graph.new data, create_counter
g.dfs(1)

1.upto(n) do |i|
  puts "#{i} #{g.d[i]} #{g.f[i]}"
end