#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const long long LINF = 1e18;
const double PI = acos(-1);
struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  long long l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  long long nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;
int c[100100];
int main() {
  int n = in.nextInt();
  vector<int> a(n), b(n);
  for (int i = (0); i < (int)(n); i++) a[i] = in.nextInt(), b[i] = in.nextInt();
  vector<pair<int, int> > p;
  for (int i = (0); i < (int)(n); i++) p.push_back({a[i], b[i]});
  sort((p).begin(), (p).end());
  int ans = 0;
  int idx1 = 0, idx2 = 0;
  for (int i = 0; i < n + 1; i++) {
    int idx =
        upper_bound((p).begin(), (p).end(), make_pair(i + 1, INF)) - p.begin();
    for (int j = idx1; j < idx; j++) {
      if (p[j].second < i) c[i]++;
      c[p[j].second]++;
    }
    if (i > 0) c[i] += c[i - 1];
    if (i > idx) continue;
    ans = max(ans, min(i, idx - c[i]));
  }
  cout << ans << endl;
}
