#include<iostream>
#include<algorithm>
using namespace std;
int a[100001],b[100001];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	sort(a,a+n);
	sort(b,b+n);
	b[n]=-1;
	int ans=1,top=0;
	int k=1;
	for(int i=0;i<n;i++){
		k++;
		while(b[top]+1==k)
			top++,k--;
		if(a[i]<=k)
			ans=max(ans,k);
	}
	cout<<ans-1<<endl;
	return 0;
}