Go: // A Way to Invite Friends(友だちの誘い方)
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=ja
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const N_MAX = 1000001

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)
	allow_list := make([]int, N_MAX+100)
	nMax := 0

	for i := 0; i < n; i++ {
		a, b := nextInt(sc), nextInt(sc)
		allow_list[a] += 1
		allow_list[b+1] += -1
		nMax = int(math.Max(float64(nMax), float64(b)))
	}

	for i := 2; i <= N_MAX; i++ {
		allow_list[i] += allow_list[i-1]
	}

	res := 0
	for i := 2; i <= nMax; i++ {
		if i-1 <= allow_list[i] { // i人誘う時に行きたい人の人数を超えないか(自分を含めて)
			res = i - 1 // 自分は除く
		}
	}

	fmt.Println(res)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

#define MAX_N

struct data_t{
    int max;
    int min;
};

struct data_a{
    int max;
    int min;
};

bool asc( const data_t& left, const data_t& right ) {
    return left.max == right.max ? left.min < right.min : left.max < right.max;
};

bool asc_a( const data_t& left, const data_t& right ) {
    return left.min == right.min ? left.max > right.max : left.min > right.min;
};

int main(){
    int n;
    cin >> n;
    cin.ignore();

    vector<data_t> data_array(n);

    for(int i=0;i<n;i++){
        cin >> data_array[i].min >> data_array[i].max;
        cin.ignore();
    }

    sort(data_array.begin(),data_array.end(),asc);

/*
    cout << "-----------------" << endl;
    for(int i=0;i<n;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
   }
*/
    int ans = n+1;
    for(int i=0;i<ans-1;i++){
        if(data_array[i].max < ans){
                        ans--;
            for(int j=i;j<ans-1;j++){
                data_array[j]=data_array[j+1];
            }
            i--;
        }
    }

    /*
    cout << "-----------------" << endl;
    for(int i=0;i<ans-1;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
    }*/

    sort(data_array.begin(),data_array.begin()+ans-1,asc_a);
/*
    cout << "KOJUN-----------------" << endl;
    for(int i=0;i<ans-1;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
    }
*/
    for(int i=0;i<ans-1;i++){
        if(data_array[i].min > ans){
             ans--;
            for(int j=i;j<ans-1;j++){
                data_array[j]=data_array[j+1];
            }
           
            i--;
        }
    }
/*
    cout << "-----------------" << endl;
    for(int i=0;i<ans-1;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
    }
*/
    cout << ans-1 << endl;

    return 0;

}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
           
#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int main()
{
    int n;
    static int new_ok[100010];
    scanf("%d", &n);
    rep (i, n)
    {
	int a, b;
	scanf("%d%d", &a, &b);
	if (a > n + 1)
	    continue;
	if (b > n + 1)
	    b = n + 1;
	++new_ok[b];
	--new_ok[a - 1];
    }
    ++new_ok[n + 1];

    int res = -3, ok = 0;
    for (int i = n + 1; i > 0; --i)
    {
	ok += new_ok[i];
	if (ok >= i)
	{
	    res = i - 1;
	    break;
	}
    }
    printf("%d\n", res);
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,a,b,ans=0;
	cin>>n;
	vi v(100005);
	rep(i,n){
		cin>>a>>b;
		v[a]++;v[b+1]--;
	}
	v[0]++;
	range(i,1,v.size()){
		v[i]+=v[i-1];
		if(i<=v[i])chmax(ans,i-1);
	}
	cout<<ans<<endl;
}