n = STDIN.gets.to_i
matrix         = Array.new(n) { Array.new(n, false) }
found_times    = Array.new(n)
complete_times = Array.new(n)

n.times do
	vertex, degree, *adj_list = STDIN.gets.split.map(&:to_i)
	degree.times do |i|
		matrix[vertex - 1][adj_list[i] - 1] = true
	end
end

time = 0
stack = []
matrix.each_index do |i|
	next unless complete_times[i].nil?

	stack.push(i)
	found_times[i] = (time += 1)

	until stack.empty?
		v   = stack.last
		adj = matrix[v].each_index { |i| break i if matrix[v][i] && found_times[i].nil? }

		# 次の隣接する頂点がない場合は上記処理で配列が返される
		if adj.is_a?(Array)
			complete_times[v] = (time += 1)
			stack.pop
			next
		end

		if found_times[adj].nil?
			found_times[adj] = (time += 1)
			stack.push(adj)
		end
	end
end

matrix.each_index { |i| puts "#{i + 1} #{found_times[i]} #{complete_times[i]}" }