#include <bits/stdc++.h>
using namespace std;
class range {
 public:
  int left, right;
  range(int left, int right) : left(left), right(right) {}
  range() {}
};
bool operator<(range A, range B) {
  if (A.left != B.left) return A.left < B.left;
  return A.right < B.right;
}
int main() {
  int N;
  cin >> N;
  int left[100002], right[100002];
  memset(left, 0, sizeof(left));
  memset(right, 0, sizeof(right));
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    left[a]++;
    right[b]++;
  }
  left[0]++;
  right[100001]++;
  int before = left[2];
  int ans = 1;
  if (left[2] >= 2) ans = 2;
  for (int num = 3; num <= N; num++) {
    int now = before + left[num] - right[num - 1];
    if (now >= num) ans = max(ans, num);
    before = now;
  }
  cout << ans - 1 << endl;
}
