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

int main(){

    int N;
    cin >> N;
    vector<int> cnt(100005, 0);

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

    vector<int> acc(100005);
    acc[0] = 0;
    for(int i = 1; i < N + 3; i++){
        acc[i] = acc[i - 1] + cnt[i];
    }

    for(int i = N + 1; i >= 0; i--){
        if(i - 1 <= acc[i]){
            cout << i - 1 << endl;
            return 0;
        }
    }

    return 0;
}
