#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<int, int> > v;
  for (int i = 0; i < n; i++) {
    pair<int, int> p;
    cin >> p.first >> p.second;
    v.push_back(p);
  }
  sort(v.begin(), v.end());
  priority_queue<int, vector<int>, greater<int> > pq;
  int ans = 1, sz = 0;
  for (int i = 0; i < n; i++) {
    int s = v[i].first;
    pq.push(v[i].second);
    sz++;
    while (pq.top() < s) {
      pq.pop();
      sz--;
    }
    if (s <= sz + 1) ans = max(ans, sz);
  }
  cout << ans - 1 << endl;
  return 0;
}
