#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int a[100000], b[100000];
  for (int i = 0; i < (n); i++) scanf("%d%d", a + i, b + i);
  int lo = 0, hi = n;
  while (lo < hi) {
    int mi = (lo + hi + 1) / 2;
    int cnt = 0;
    for (int i = 0; i < (n); i++)
      if (a[i] <= 1 + mi && 1 + mi <= b[i]) cnt++;
    if (mi <= cnt)
      lo = mi;
    else
      hi = mi - 1;
  }
  printf("%d\n", lo);
  return 0;
}
