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 <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i,n)	for(int i=0;i<n;i++)
#define srep(i,a,n)	for(int i=a;i<n;i++)
#define REP(i,n)	for(int i=0;i<=n;i++)
#define SREP(i,a,n)	for(int i=a;i<=n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define fst	first
#define scn second

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;

int main() {
	int n;	cin >> n;
	vint a(n, 0), b(n, 0);
	rep(i, n)	cin >> a[i] >> b[i];
	sort(b.begin(), b.end());
	cout << b[0] - 1 << endl;
	return 0;
}

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

using namespace std;

int main() {
    int N;
    int a,b;
    int sum=0;
    int max=0;;
    cin >> N;
    vector<int> vect(100003,0);
    for(int i=0; i<N ; i++){
        cin >> a >> b;
        vect[a]++;
        vect[b+1]--;
    }
    for(int i=1; i<100003; i++){
        sum += vect[i];
        if(sum > max) max=sum;
    }
    cout << max << endl;
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i,n)	for(int i=0;i<n;i++)
#define srep(i,a,n)	for(int i=a;i<n;i++)
#define REP(i,n)	for(int i=0;i<=n;i++)
#define SREP(i,a,n)	for(int i=a;i<=n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define fst	first
#define scn second

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;

int bit[100010];
int N = 0;

void init(int n) {
	N = n;
	rep(i, N)	bit[i] = 0;
}

void add(int k, int a) {
	while (k <= N) {
		bit[k] += a;
		k += k&-k;
	}
}

int sum(int k) {
	int ret = 0;
	while (k > 0) {
		ret += bit[k];
		k -= k&-k;
	}
	return ret;
}

int main() {
	int n;	cin >> n;
	vector<pii> a(n, mp(0, 0));
	rep(i, n)	cin >> a[i].fst >> a[i].scn;
	sort(a.begin(), a.end());
	//????°??????°???n????????§??????????????¶??????????????§?????????
	while (a.size() > 0 && n < a[n - 1].fst)	a.erase(a.begin() + (--n));
	int big = 0;
	rep(i, n)	big = max(big, a[i].scn);
	big++;
	init(big);
	//????°??????°????°??????????????????£??????????????§????????§?????°???BIT??§??????
	int ret = 1;
	rep(i, n) {
		add(a[i].second,1);
		ret = max(ret, i + 2 - sum(a[i].fst));
	}
	cout << ret - 1 << endl;
	return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T &a, const T &b) {return a = min(a, b);}
template <typename T> T &chmax(T &a, const T &b) {return a = max(a, b);}

using ll = long long;
using ld = long double;

const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);

int N;
using pii = pair<int, int>;
int num[100005];

int main(){
	cin >> N;
	vector<pii> req; 
	REP(cnt, N) {
		int a, b;
		cin >> a >> b;
		num[a]++; num[b+1]--;
	}
	num[0]++; num[100000]--;
	REP(i, N+1) num[i+1] += num[i];
	int res = 0;
	REP(i, N+2) if(num[i] >= i) res = max(res, i);
	cout << res-1 << endl;
	return 0;

}