#include <iostream>

#define REP(i,n) for (int i=0; i<n; i++)


using namespace std;

int main(){
	int n, a, b, fr[100005], ans=1;
	cin >> n;
	REP(i,100005){ fr[i] = 0;}
	fr[0] = 1;
	REP(i,n){
		cin >> a >> b;
		fr[a]++;
		fr[b+1]--;
	}
	REP(i,100004){
		fr[i+1] += fr[i];
	}
	for (int i=0; i<=n+1; i++){
		if (fr[i]>ans && fr[i]>=i){
			ans = min(i, fr[i]);
		}
	}
	
	
	//REP(i,10) cerr << fr[i] << " ";
	//cerr << endl;
	
	cout << ans-1 << endl;
	
}
