#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, left, right, maximum;
  scanf("%d", &N);
  int* table = new int[100003];
  for (int i = 0; i <= 100002; i++) {
    table[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &left, &right);
    table[left]++;
    table[right + 1]--;
  }
  maximum = 0;
  for (int i = 1; i <= N + 1; i++) {
    table[i] += table[i - 1];
    if (table[i] >= i) {
      maximum = table[i];
    }
  }
  printf("%d\n", min(N - 1, max(maximum - 1, 0)));
  return 0;
}
