#include <bits/stdc++.h>
using namespace std;
int N;
int main() {
  cin >> N;
  int memo[101010];
  fill(memo, memo + 101010, 0);
  for (int j = 0; j < N; ++j) {
    int a, b;
    cin >> a >> b;
    ++memo[a];
    --memo[b + 1];
  }
  int ret = 1;
  for (int j = 2; j < 101010; ++j) {
    memo[j] += memo[j - 1];
    if (memo[j] >= j - 1) {
      ret = max(ret, memo[j]);
    }
  }
  cout << ret - 1 << endl;
  return 0;
}
