#include <bits/stdc++.h>
using namespace std;
int num[100005];
int num2[100005];
int main() {
  int N, a, b;
  cin >> N;
  for (int n = 0; n < N; n++) {
    cin >> a >> b;
    num[a]++;
    num[b + 1]--;
  }
  int ans = num2[0] = num[0];
  for (int i = 1; i < 100005; i++) {
    num2[i] = num2[i - 1] + num[i];
    if (num2[i] >= i) ans = max(ans, num2[i]);
  }
  ans--;
  cout << max(0, ans) << endl;
}
