#include <bits/stdc++.h>
using namespace std;
int fav[100005];
int main() {
  int N;
  cin >> N;
  memset(fav, 0, sizeof(fav));
  for (int i = 0; i < N; i++) {
    int l, r;
    cin >> l >> r;
    fav[l]++;
    fav[r]--;
  }
  int res = 0, mem = 1;
  for (int i = 1; i <= N + 1; i++) {
    mem += fav[i];
    if (mem >= i) res = i;
  }
  cout << res - 1 << endl;
}
