#include <bits/stdc++.h>
using namespace std;
int main(void) {
  vector<int> total(1e05 + 3, 0);
  vector<int> start(1e05 + 3, 0);
  vector<int> goal(1e05 + 3, 0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int s;
    int g;
    cin >> s >> g;
    start[s]++;
    goal[g + 1]++;
  }
  total[0] = 0;
  for (int i = 1; i < 1e05 + 3; i++) {
    total[i] = total[i - 1] + start[i] - goal[i];
  }
  int rmax = 0;
  for (int i = 1; i <= n + 1; i++) {
    rmax = max(rmax, min(total[i], i) - 1);
  }
  cout << rmax << endl;
  return 0;
}
