#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
int main(void) {
  int N;
  cin >> N;
  std::vector<int> frimin(N);
  std::vector<int> frimax(N);
  for (int i = 0; i < N; i++) {
    cin >> frimin[i] >> frimax[i];
  }
  std::sort(frimin.begin(), frimin.end());
  std::sort(frimax.begin(), frimax.end());
  int ret = 0;
  int count = 1;
  int imin = -1;
  int imax = -1;
  for (int f = 2; f < N + 1 && f < frimax[N - 1]; f++) {
    for (int i = imin + 1; frimin[i] == f; i++) {
      count++;
      imin++;
    }
    for (int i = imax + 1; frimax[i] + 1 == f; i++) {
      count--;
      imax++;
    }
    if (count >= f && count != 1) {
      ret = f - 1;
    }
  }
  cout << ret << endl;
  return 0;
}
