#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	int i;
	int N=read();
	int a[100002]={0},b[100002]={0};
	int sum[N+1]={0};
	
	sum[0] = 1; //include me
	
	REP(i,N){
		a[read()] ++;
		b[read()] --;
	}
	
	for(i=1;i<=N;i++){
		sum[i] += sum[i-1] + a[i] + b[i];
		//cout << sum[i] << endl;
	}
	
	for(i=N;i>=0;i--){
		if(sum[i]>=i){
			cout << i-1;
			return 0;
		}
	}
	cout << 0;
	return 0;
}