#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int num[100000][2];
  int maxa, minb;
  int i, j, k;
  int indexa, indexb;
  cin >> n;
  for (i = 0; i < n; i++) cin >> num[i][0] >> num[i][1];
  for (k = 0; k < n; k++) {
    maxa = num[0][0];
    minb = num[0][1];
    indexa = indexb = 0;
    for (i = 1; i < n; i++) {
      if (maxa < num[i][0]) {
        maxa = num[i][0];
        indexa = i;
      }
      if (minb > num[i][1]) {
        minb = num[i][1];
        indexb = i;
      }
    }
    if (maxa <= n - k + 1 && n - k + 1 <= minb) {
      cout << n - k << endl;
      break;
    } else if (maxa <= n - k + 1) {
      num[indexb][0] = 0;
      num[indexb][1] = 100001;
    } else if (minb >= n - k + 1) {
      num[indexa][0] = 0;
      num[indexa][1] = 100001;
    }
  }
  if (k > n - 2) cout << "0" << endl;
}
