#include <bits/stdc++.h>
using namespace std;
int c[100100];
int main() {
  int N;
  cin >> N;
  int a[N], b[N];
  for (int i = 0; i < N; i++) cin >> a[i] >> b[i];
  for (int i = 0; i < N; i++) {
    c[a[i]]++;
    c[b[i] + 1]--;
  }
  for (int i = 1; i < 100100; i++) {
    c[i] += c[i - 1];
  }
  int s = 0;
  for (int i = 1; i < 100100; i++) {
    if (c[i] >= i) {
      s = max(s, c[i] - 1);
    }
  }
  cout << s << endl;
  return 0;
}
