#include <iostream>
#include <algorithm>

using namespace std;

int c[2][100002], n, a, b;

int main(){
  cin >> n;
  fill(c[0], c[2], 0);
  for(int i=0;i<n;i++){
    cin >> a >> b;
    c[0][a]++;
    c[1][b]++;
  }
  a = 0;
  b = 1;
  for(int i=1;i<n+3;i++){
    b += c[0][i];
    b -= c[1][i-1];
    if(b >= i) a = i - 1;
  }
  cout << a << endl;
}