class Heap
	def initialize
		@array = []
	end

	def add(obj, key)
		@array << [obj, key]
		lift_up(@array.size - 1)
	end

	def swap(i, j)
		tmp = @array[i]
		@array[i] = @array[j]
		@array[j] = tmp
	end

	def lift_up(i)
		return if i == 0
		j = (i - 1) / 2
		if @array[i][1] < @array[j][1]
			swap(i, j)
			lift_up(j)
		end
	end

	def del_min
		min = @array[0]
		if size > 1
			@array[0] = @array.pop
			lift_down(0)
		else
			@array.pop
		end
		min
	end

	def min_obj
		@array[0]
	end

	def min_key
		@array[1]
	end

	def lift_down(i)
		j = 2 * i + 1
		k = j + 1
		if k >= size
			return if j >= size
			if @array[i][1] > @array[j][1]
				swap(i, j)
				lift_up(j)
			end
		else
			l = (@array[j][1] < @array[k][1] ? j : k)
			if @array[i][1] > @array[l][1]
				swap(i, l)
				lift_up(l)
			end
		end
	end

	def empty?
		@array.empty?
	end

	def size
		@array.size
	end
end

n = gets.to_i
friends = (1..n).map{ gets.split(/\s+/).map(&:to_i) }.sort

queue = Heap.new
max = 0

(1..n).each{|k|
	while !friends.empty? && friends[0][0] == k
		a, b = friends.shift
		queue.add(a, b)
	end
	
	max = k - 1 if queue.size >= k - 1
	
	while !queue.empty? && queue.min_key == k 
		queue.del_min
	end
}

puts max