#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, a, b;
  cin >> n;
  vector<int> sum(n + 3, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    if (a <= n + 1) ++sum[a - 1];
    if (b <= n + 1) --sum[b];
  }
  for (int i = 1; i <= n; ++i) sum[i] += sum[i - 1];
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (i <= sum[i]) ans = max(ans, min(i, sum[i]));
  cout << ans << endl;
  return 0;
}
