#include<bits/stdc++.h>
using namespace std;

int imos[100005]={0};

int main(){
    int n;
    cin >> n;
    for(int i=0;i<n;++i){
        int a, b;
        cin >> a >> b;
        imos[a]++;
        imos[b+1]--;
    }
    imos[0]++;
    int acc = 0, maxi = 0;
    for(int i=0;i<=100001;++i){
        acc += imos[i];
        if(acc >= i)maxi = max(maxi, min(i, acc));
    }

    cout << maxi - 1 << endl;

    return 0;
}

