#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T>
using VT = vector<T>;
const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
void solve(int N) {
  VI seg(100010, 0);
  for (int(i) = 0; (i) < (N); (i)++) {
    int a, b;
    scanf("%d%d", &a, &b);
    seg[a]++;
    seg[b + 1]--;
  }
  for (int(i) = 0; (i) < (100008); (i)++) seg[i + 1] += seg[i];
  int ans = 0;
  for (int(i) = (1); (i) < (N + 1); (i)++) {
    if (seg[i] >= i - 1) ans = i - 1;
  }
  cout << ans << endl;
  return;
}
signed main(void) {
  LL n, m, p, a, b, c, x, y, z, q;
  string s;
  bool f = false;
  while (cin >> n, n) {
    solve(n);
    return 0;
  }
}
