#include <bits/stdc++.h>
using namespace std;
int N;
int a[114514];
int b[114514];
bool isPossible(int n) {
  if (n > 100000) return false;
  int count = 0;
  for (int i = 0; i < N; i++) {
    if ((a[i] <= n + 1) && (n + 1 <= b[i])) {
      count++;
    }
  }
  return (count >= n);
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i];
  }
  int l = 1;
  int u = 100001;
  while (true) {
    if (l + 1 == u) {
      break;
    }
    int mid = (l + u) / 2;
    if (isPossible(mid)) {
      l = mid;
    } else {
      u = mid;
    }
  }
  cout << l << endl;
}
