#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int imos[1000001];
  for (long long i = 0; i < (long long)(1000001); i++) imos[i] = 0;
  for (long long i = 0; i < (long long)(n); i++) {
    int a, b;
    cin >> a >> b;
    imos[a]++;
    imos[b + 1]--;
  }
  for (long long i = 0; i < (long long)(1000000); i++) imos[i + 1] += imos[i];
  int ans = 0;
  for (long long i = 0; i < (long long)(n + 1); i++)
    if (imos[i] + 1 > i) ans = max(ans, min(imos[i], (int)i - 1));
  cout << ans << endl;
}
