Vertex = Struct.new(:id, :adj_list, :found_time, :completed_time) do
	def to_s
		"#{id} #{found_time} #{completed_time}"
	end
end

def dfs!(vertex, time)
	return time unless vertex.found_time.nil?

	vertex.found_time ||= time
	time += 1

	time = vertex.adj_list.inject(time) { |t, v| dfs!(v, t) }
	vertex.completed_time = time
	time += 1
end

n = STDIN.gets.to_i
vertex_list = Array.new(n) { |i| Vertex.new(i + 1) }
n.times do
	node_no, degree, *adj_list = STDIN.gets.split.map(&:to_i)

	vertex = vertex_list[node_no - 1]
	vertex.adj_list = adj_list[0...degree].map! { |adj| vertex_list[adj - 1] }
end

time = 1
vertex_list.each do |v|
	time = dfs!(v, time)
	puts v
end