#include <bits/stdc++.h>
using namespace std;
int main(void) {
  for (;;) {
    int n;
    cin >> n;
    if (!n) return 0;
    vector<int> f(131072, 0);
    for (int i = (0); i < (n); ++i) {
      int l, r;
      cin >> l >> r;
      ++f[l];
      --f[r + 1];
    }
    ++f[0];
    for (int i = (0); i < (131000); ++i) f[i + 1] += f[i];
    int answer = 0;
    for (int i = (0); i < (131000); ++i)
      if (f[i] >= i) answer = i - 1;
    cout << answer << endl;
  }
}
