#include <iostream>
#include <utility>
#include <algorithm>
#include <queue>

#define P pair<int, int>
using namespace std;

int N;
pair<int, int> ab[100005];
int end[100005] = {0};

priority_queue< P, vector< P >, greater< P > > Q;

int main(void)
{
	cin >> N;
	for(int i = 1; i <= N; i++){
		cin >> ab[i].first >> ab[i].second;
	}
	
	for(int i = 1; i <= N; i++){
		Q.push( ab[i] );
	}
	
	int num = 0;
	int max = 0;
	
	
	for(int i = 1; i <= 100005; i++){
		
		
		
		num -= end[i];
		while(Q.top().first == i && (int)Q.size()){
			num++;
			end[Q.top().second + 1]++;
			Q.pop();
		}
		
		if(num >= i-1) max = i;
		if((int)Q.size() == 0) break;
		
	}
	
	end:
	cout << max-1 << endl;
	return 0;
}