#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[100005] = {};
  int n, x, y, res = 0;
  a[1] = 1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    a[x]++;
    a[y]--;
  }
  for (int i = 0; i < 100005; i++) {
    a[i + 1] += a[i];
  }
  for (int i = 0; i < 100005; i++) {
    if (a[i] >= i) {
      res = max(res, a[i]);
    }
  }
  cout << res - 1 << endl;
  return 0;
}
