#include <bits/stdc++.h>
using namespace std;
const long long hmod1 = 999999937;
const long long hmod2 = 1000000000 + 9;
const long long INF = 1 << 30;
const long long mod = 10000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;
int n;
int a[100000 + 5], b[100000 + 5];
int imos[100000 + 5];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a[i] >> b[i];
    imos[a[i]]++;
    imos[b[i] + 1]--;
  }
  for (int i = 0; i < (100000 + 3); i++) imos[i + 1] += imos[i];
  int ans = 0;
  for (int i = 0; i < (100000 + 5); i++) {
    if (i == imos[i]) ans = max(ans, imos[i] - 1);
  }
  cout << ans << endl;
}
