#include <bits/stdc++.h>
int main() {
  int a[100002] = {0};
  int b[100002] = {0};
  int N;
  int i, k;
  int answer = 0;
  int p;
  scanf("%d", &N);
  for (i = 1; i <= N; i++) scanf("%d %d", &a[i], &b[i]);
  for (k = 0; k <= N; k++) {
    p = 0;
    for (i = 1; i <= N; i++) {
      if (a[i] <= k && k <= b[i]) p++;
    }
    if (p == k)
      answer = p - 1;
    else if (p == k - 1)
      answer = p;
  }
  printf("%d\n", answer);
  return 0;
}
