#include <bits/stdc++.h>
using namespace std;
int n;
int a[100000], b[100000];
int solve() {
  int ok = 0, ng = 100001;
  while (abs(ok - ng) > 1) {
    int m = (ok + ng) / 2;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] <= m + 1 && m + 1 <= b[i]) cnt++;
    }
    if (cnt >= m)
      ok = m;
    else
      ng = m;
  }
  return ok;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cout << solve() << endl;
  }
}
