$visit = 0
$visited = []
$stack = []
class Vertex
  @@index = 0
  def initialize(adjacent)
    @index = @@index += 1
    @adjacent = adjacent
    @discovery = nil
    @finishing = nil
  end

  def <=>(vertex)
    self.index <=> vertex.index
  end

  def to_s
    "#{index} #{discovery} #{finishing}"
  end

  attr_reader :index
  attr_accessor :adjacent, :finishing, :discovery
end

def dfs(vertex)
  return if vertex.nil?
  if vertex.discovery.nil?
    vertex.discovery = $visit += 1
    $visited << vertex.index
  end
  if vertex.adjacent.empty? || vertex.adjacent.all? { |x| !x.discovery.nil? }
    vertex.finishing = $visit += 1 if vertex.finishing.nil?
    dfs $stack.pop
  else
    $stack << vertex
    dfs vertex.adjacent.delete vertex.adjacent.find { |x| !$visited.include?(x.index) }
  end
end

n = gets.to_i
array = Array.new(n).map { Vertex.new([]) }
n.times do
  adjacent = []
  input = gets.split(' ').map(&:to_i)
  unless input[1].zero?
    input[2..-1].each do |index|
      adjacent << array.find { |vertex| vertex.index == index }
    end
    array[input[0] - 1].adjacent = adjacent
  end
end
# str =<<EOS
# 1 2 2 4
# 2 1 5
# 3 2 5 6
# 4 0
# 5 1 4
# 6 1 6
# EOS
# str.each_line do |a|
#   adjacent = []
#   input = a.split(' ').map(&:to_i)
#   unless input[1].zero?
#     input[2..-1].each do |index|
#       adjacent << array.find { |vertex| vertex.index == index }
#     end
#     array[input[0] - 1].adjacent = adjacent
#   end
# end
array.each do |val|
  dfs val if val.discovery.nil?
end
array.map { |vertex| puts vertex.to_s }
