#include <iostream>
#include <algorithm>
using namespace std;

int main() {

    int n, imos[100003] = {0};

    imos[0] = 1;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int a, b; cin >> a >> b;
        imos[a]++, imos[b + 1]--;
    }

    for(int i = 1; i < 100003; i++) {
        imos[i] += imos[i - 1];
    }

    int maxi = 0;
    for(int i = 0; i <= n + 1; i++) {
        if(imos[i] >= i) maxi = max(maxi, i);
    }
    cout << maxi - 1 << endl;
}

