#include<bits/stdc++.h>
#define N 100002
using namespace std;
int n,a,b,d[N],ans;

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    for(int j=a;j<=b;j++)d[j]++;
  }
  ans=1;
  for(int i=n+1;i>=2;i--)
    if(d[i]>=i-1)ans=max(ans,i);
  cout<<ans-1<<endl;
  return 0;
}