#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
    int N; cin >> N;
    vector<int> num(N + 3);
    rep(i, N) {
        int a, b; cin >> a >> b;
        if (a > N + 1) continue;
        num[a] += 1; num[min(b, N + 1) + 1] -= 1;
    }
    int maxi = 0;
    for (int i = 2; i < N + 3; ++i) {
        num[i] += num[i - 1];
        if (num[i] >= i - 1 && maxi < i - 1) maxi = i - 1;
    }
    cout << maxi << endl;
}