#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, ans = 1;
  cin >> N;
  int A[100002] = {0};
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    A[a]++, A[b + 1]--;
  }
  for (int i = 3; i < N + 1; i++) {
    A[i] += A[i - 1];
  }
  for (int i = N; N >= 2; N--) {
    if (A[i] >= i) {
      ans = i;
      break;
    }
  }
  cout << (ans - 1) << endl;
  return 0;
}
