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