#include <bits/stdc++.h>
using namespace std;
long long x[100100];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (long long i = 0; i < 100100; i++) x[i] = 0;
  long long N;
  cin >> N;
  for (long long i = 0; i < N; i++) {
    long long a, b;
    cin >> a >> b;
    if (a > N + 1) continue;
    for (long long j = a; j <= min(b, N + 1); j++) {
      x[j]++;
    }
  }
  long long ans = 0;
  for (long long i = 0; i <= N + 1; i++) {
    if (x[i] + 1 >= i) ans = max(ans, max(i, x[i]));
  }
  ans = max(0LL, ans - 1);
  cout << ans << endl;
}
