#include <bits/stdc++.h>
int a[100000], b[100000], c[100000];
using namespace std;
int main(void) {
  int i, j, k, n, d = 0, ret = 0;
  cin >> n;
  for (i = 0; i < n; i++) cin >> a[i] >> b[i];
  sort(a, a + n);
  sort(b, b + n);
  for (i = 2, j = 0, k = 0; i <= n + 1; i++) {
    while (a[j] == i) {
      j++;
      d++;
    }
    while (b[k] == i - 1) {
      k++;
      d--;
    }
    if (i <= d) ret = i - 1;
  }
  cout << ret << endl;
  return 0;
}
