#include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  vector<int> d(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
      c[a]++;
      c[b+1]--;
  }
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i <= c[i]+1) maxnum = i-1;
  }
  cout << maxnum << endl;
  return 0;
}