#include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<(S)<<endl
#define ck(a,b) (0<=(a)&&(a)<(b))

using namespace std;

int main(){
	int N,ans=1;
	cin>>N;
	int A[100002]={0};
	A[2]++;
	REP(i,0,N){
		int a,b;
		cin>>a>>b;
		A[a]++,A[b+1]--;
	}
	REP(i,3,N+2){
		A[i]+=A[i-1];
	}
	for(int i=N+1;i>=2;i--){
		if(A[i]>=i){
			ans=i;
			break;
		}
	}
	p(ans-1);
	return 0;
}