#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  int maxn = 0;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> a[i] >> b[i];
    maxn = max(b[i], maxn);
  }
  sort((a).begin(), (a).end());
  sort((b).begin(), (b).end());
  auto ita = a.begin();
  auto itb = b.begin();
  int res = 0;
  int cnt = 0;
  for (int i = 0; i < (int)(maxn); ++i) {
    while (*ita == i) {
      ita++;
      cnt++;
    }
    while (*itb == i - 1) {
      itb++;
      cnt--;
    }
    if (i - 1 <= cnt) {
      res = max(res, i - 1);
    }
  }
  cout << res << endl;
}
