# なんかめんどくさいしやりにくいな
# 隣接リストが探す順番なのでstackを使わずイテレータで回す
n = gets.chomp.to_i
@data = {}
@endtime = {}
@starttime = {}
@used = []
n.times do
  u, _, *k = gets.chomp.split.map(&:to_i)
  @data[u] = k
end
@time = 1
def dfs(id)
  @used << id
  @starttime[id] = @time
  @time+=1
  if @data[id].empty?
    # 終了
    @endtime[id] = @time # ?
  else
    @data[id].each do |next_id|
      next if @used.include?(next_id)
      dfs(next_id)
      @time+=1  # ?
    end
    @endtime[id] = @time
  end
end
@used << 1
dfs(1)
n.times do |i|
  i+=1
  puts "#{i} #{@starttime[i]} #{@endtime[i]}"
end
