#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
  int ok = 0, ng = n + 1;
  int t = 20;
  while (t--) {
    int mid = (ok + ng) / 2;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] <= mid + 1 && mid + 1 <= b[i]) cnt++;
    }
    if (cnt >= mid)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}
