#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> p[200100];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;
  p[n] = pair<int, int>(1, 100001);
  sort(p, p + n + 1);
  priority_queue<int, vector<int>, greater<int> > q;
  int ans = 1;
  for (int i = 0; i <= n; i++) {
    while (!q.empty() && q.top() < p[i].first) q.pop();
    q.push(p[i].second);
    if (p[i].first <= q.size() && q.size() <= q.top())
      ans = max(ans, (int)q.size());
  }
  cout << ans - 1 << endl;
}
