#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int imos[100010];
int main() {
  int N;
  scanf("%d", &N);
  int a, b;
  memset(imos, 0, sizeof(imos));
  for (int i = (int)(0); i < (int)(N); ++i) {
    scanf("%d%d", &a, &b);
    imos[a]++;
    imos[b + 1]--;
  }
  for (int i = (int)(0); i < (int)(N + 1); ++i) imos[i + 1] += imos[i];
  for (int i = (int)(0); i < (int)(N + 1); ++i)
    if (imos[i] < i - 1) imos[i] = 0;
  int ans = 0;
  for (int i = (int)(0); i < (int)(N + 1); ++i) ans = max(ans, imos[i]);
  printf("%d\n", max(ans - 1, 0));
  return 0;
}
