#include<bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
int main(){
  int n;cin>>n;
  int rui[100010]={};
  for(int i=0;i<n;i++){
    int a,b;cin>>a>>b;
    rui[a]++;
    rui[b+1]--;
  }
  int ma=1;
  int now=1;
  for(int i=2;i<100010;i++){
    now+=rui[i];
    //cout<<now<<endl;
    if(now>=i){
    ma=max(ma,i);
    }
  }
  
  cout<<ma-1<<endl;
  return 0;
}

