#! /usr/bin/ruby -w
# encoding: utf-8
#

class Result < Struct.new(:found, :finish)
  def complete?
    found and finish
  end

  def to_s
    "%d %d" % [found ? found : 0, finish ? finish : 0]
  end
end

class MyTime
  def initialize
    @value = 0
  end

  def inc
    @value = @value + 1
  end

  def to_s
    @value
  end

  attr_reader :value
end

def search(num, graph, time, result)
  unless result[num].complete?
    result[num].found = time.inc
    row = graph[num]
    row.each_with_index do |e, idx|
      next if idx == num
      next unless result[idx].found.nil? # 訪れたことがある場合
      if e == 1
        search(idx, graph, time, result)
      end
    end
    result[num].finish = time.inc
  end
end

if __FILE__ == $0
  n = gets.to_i
  g = []
  n.times {
    a = Array.new(n, 0)
    info = gets.split.map(&:to_i)
    info[1].times do |idx|
      outdegree = info[idx + 2]
      a[outdegree - 1] = 1
    end
    g << a
  }

  time = MyTime.new
  result = Array.new(n).map { Result.new }
  n.times do |num|
    search(num, g, time, result)
  end

  result.each_with_index do |r, idx|
    puts "%d %s" % [idx + 1, r]
  end
end

