#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  vector<pair<long long int, long long int> > v;
  for (long long int i = 0; i < n; i++) {
    long long int a, b;
    cin >> a >> b;
    v.push_back(make_pair(0, a));
    v.push_back(make_pair(1, b));
  }
  sort(v.begin(), v.end());
  long long int ans = 1;
  long long int cnt = 1;
  bool f = false;
  for (long long int i = 0; i < v.size(); i++) {
    if (v[i].first == 0) {
      cnt++;
      if (cnt >= v[i].second) f = true;
    } else {
      if (f == true) {
        ans = max(ans, min(cnt, v[i].second));
      }
      cnt--;
      if (cnt < v[i].second) f = false;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
