#include <bits/stdc++.h>
using namespace std;
int mymax(int a, int b) { return (a > b) ? a : b; }
int main(void) {
  int N;
  cin >> N;
  int a[N];
  int b[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    cin >> b[i];
  }
  int ans[N + 1];
  for (int i = 0; i < N + 1; i++) {
    ans[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    for (int j = a[i]; j <= b[i] && j <= N; j++) {
      if (ans[j] < j) {
        ans[j]++;
      } else {
      }
    }
  }
  int max = -1;
  for (int i = 0; i < N + 1; i++) {
    if (max < ans[i]) {
      max = ans[i];
    }
  }
  cout << mymax(max - 1, 0) << endl;
}
