#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, a, b, ans, s, t;
  cin >> N;
  vector<int> A, B;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  ans = 0;
  vector<int>::iterator itr1, itr2;
  for (int i = N; i >= 0; --i) {
    itr1 = upper_bound(A.begin(), A.end(), i + 1);
    itr2 = lower_bound(B.begin(), B.end(), i + 1);
    s = distance(A.begin(), itr1) + 1;
    t = distance(B.begin(), itr2);
    if (min(s, N - t) == i + 1) {
      ans = min(s, N - t) - 1;
      break;
    }
  }
  cout << ans << endl;
}
