#include <bits/stdc++.h>
using namespace std;
template <class T>
struct SegmentTree {
  vector<T> nodes, stocks;
  int size;
  SegmentTree(int size) : size(size) {
    nodes.resize(size * 4, 0);
    stocks.resize(size * 4, 0);
  }
  void maintain_consistency(int pos) {
    if (stocks[pos] > 0) {
      stocks[pos * 2 + 1] += stocks[pos] / 2;
      stocks[pos * 2 + 2] += stocks[pos] / 2;
      nodes[pos] += stocks[pos];
      stocks[pos] = 0;
    }
  }
  T get_inner(int left, int right, int pos, int pl, int pr) {
    if (pr <= left || right <= pl) return 0;
    if (left <= pl && pr <= right) return nodes[pos] + stocks[pos];
    maintain_consistency(pos);
    const int center = (pl + pr) / 2;
    T lv = get_inner(left, right, pos * 2 + 1, pl, center);
    T rv = get_inner(left, right, pos * 2 + 2, center, pr);
    return lv + rv;
  }
  T get(int left, int right) { return get_inner(left, right, 0, 0, size); }
  T add_inner(int left, int right, int pos, int pl, int pr) {
    if (pr <= left || right <= pl) return stocks[pos] + nodes[pos];
    if (left <= pl && pr <= right) {
      stocks[pos] += pr - pl;
      return stocks[pos] + nodes[pos];
    }
    maintain_consistency(pos);
    const int center = (pl + pr) / 2;
    T lv = add_inner(left, right, pos * 2 + 1, pl, center);
    T rv = add_inner(left, right, pos * 2 + 2, center, pr);
    return nodes[pos] = lv + rv;
  }
  T add(int left, int right) { return add_inner(left, right, 0, 0, size); }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  SegmentTree<int> segtree(100002);
  for (int i = 0; i < N; ++i) {
    int minval, maxval;
    cin >> minval >> maxval;
    segtree.add(minval - 1, maxval);
  }
  int ans = 0;
  for (int n = N; n >= 0; --n) {
    const int val = segtree.get(n, n + 1);
    if (val >= n) {
      ans = n;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
