#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  int n;
  while (cin >> n) {
    int acc[100010] = {};
    for (int i = 0; i < (int)(n); ++i) {
      int a, b;
      cin >> a >> b;
      ++acc[a];
      --acc[b + 1];
    }
    int x = 0;
    int ans = 0;
    for (int i = 0; i < (int)(100005); ++i) {
      x += acc[i];
      if (x == i) ans = max(ans, x - 1);
    }
    cout << ans << endl;
  }
}
