#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<int, int> > invert(n);
  int nmax = -10, nmin = (int)1e7;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    invert[i].first = b;
    invert[i].second = a;
    nmin = min(nmin, a);
    nmax = max(nmax, b);
  }
  sort(invert.begin(), invert.end());
  int ans = 0;
  for (int i = nmax; i >= nmin; i--) {
    vector<pair<int, int> >::iterator up;
    pair<int, int> mp(i, 0);
    up = lower_bound(invert.begin(), invert.end(), mp);
    int lower = distance(invert.begin(), up);
    int c = 0;
    for (int j = lower; j < invert.size(); j++) {
      if (invert[j].second <= i) {
        c++;
      }
    }
    if (c >= i) {
      ans = c;
      break;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
