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

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define ll long long
#define P pair<ll,ll>
const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1);

int main(void)
{
    ll n; cin>>n;
    vector<ll> s(1e6,0);
    rep(i,n){
        ll a,b; cin>>a>>b;
        s[--a]++;
        s[b]--;
    }
    ll ans=0;
    for(ll i=1;i<=n;++i){
        s[i]+=s[i-1];
        if(i<=s[i]) ans=i;
    }
    cout<<ans<<endl;
    return 0;
}

