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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e17;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rep(i,N) for(ll i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

double dist(Point p) { return abs(p); }//ベクトルpの絶対値
double dist(Point a, Point b) {//2点間の距離
	return abs(a - b);
}

int n;
int sum[100100];

int main() {
	cin >> n;
	rep(i, n) {
		int a, b;
		cin >> a >> b;
		sum[a]++;
		sum[b + 1]--;
	}
	rep(i, 100100 - 1)sum[i + 1] += sum[i];
	ll ans = 0;
	rep(i, n + 1) {
		ll num = i + 1;
		if (sum[num] >= i)ans = max(ans, i);
	}
	cout << ans << endl;
}

C++: //#include "bits/stdc++.h"

#include <iostream>
#include <array>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

array<int, 100002> box; // use key 100001

int n;
int np;
int min_;
int max_;

int solve() {
	cin >> n;
	np = n + 1;
	for (int i = 0; i < n; i++) {
		cin >> min_ >> max_;
		if (min_ > np) {
			continue;
		}
		box[min_ - 1]--;
		max_ = min(max_, np);
		box[max_]++;
	}
	box[np + 2] = 0;
	for (int i = np + 1; i >= 0; i--) {
		box[i] += box[i+1];
		std::cout << "debug " << i << " " << box[i] << std::endl; // debug
		if (i - 1 <= box[i]) {
			return i - 1;
		}
	}
	return 0;
}

int main() {

	cout << solve() << endl;

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	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-1]++;
        vect[b]--;
    }
    for(int i=1; i<100003; i++){
        sum += vect[i];
        if(sum > max && sum >= i) max=i;
    }
    cout << max << endl;
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio> 
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rangeRep(i, n, m) for(int (i) = (n); (i) < (m); (i)++)

int canGo[100001] = {0};

class Solve {
public:
	Solve() {};
	~Solve() {};

	void solver() {
		int ans = 0;
		int n;
		std::cin >> n;
		std::vector<std::pair<int, int>> friends(n, std::make_pair(0, 0));
		rep(i, n) {
			std::cin >> friends[i].first >> friends[i].second;
			//Exclude myself
			friends[i].first--; friends[i].second--;
		}
		//Record the number of people who can go.
		for (auto& x : friends) {
			rangeRep(i, x.first, x.second+1) {
				canGo[i]++;
			}
		}
		int max = 0;
		rep(i, n+1) {
			if (canGo[i] >= i) {
				max = i;
			}
		}
		std::cout << max << std::endl;
	}
private:

};

int main() {
	Solve solve;
	solve.solver();
	return 0;
}

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

using namespace std;

int main(){
  int num[100005]={0};
  int n,m=0,temp;
  cin>>n;
  int a,b;
  //num[0]++;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    //for(int j=a;j<=b;j++){
    //num[j]++;
    //}
    num[a]++;
    num[b+1]--;
  }

  for(int i=2;i<10005;i++){
    num[i]+=num[i-1];
    if(num[i]+1>=i)
      temp=i-1;
  }
  cout<<temp<<endl;
  //for(int i=10004;i>0;i--){
  //if(num[i]>=i){
  //  cout<<i-1<<endl;
    // return 0;
    //}
  //}
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int main()
{
    int n;
    cin >> n;
    int result[100003] = {};
    for(int i = 0; i < n; i++){
        int l, r;
        cin >> l >> r;
        result[l]++;
        result[r + 1]--;
    }
    int ans = 0;
    for(int i = 1; i < n; i++){
        result[i] += result[i - 1];
        // cout << result[i] << endl;
        if(result[i] + 1 >= i) ans = max(i + 1, ans);
    }
    ans--;
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<(S)<<endl
#define ck(a,b) (0<=(a)&&(a)<(b))

using namespace std;

int main(){
	int N,ans=1;
	cin>>N;
	int A[100002]={0};
	A[2]++;
	REP(i,0,N){
		int a,b;
		cin>>a>>b;
		A[a]++,A[b+1]--;
	}
	REP(i,3,N+1){
		A[i]+=A[i-1];
	}
	for(int i=N+1;i>=2;i--){
		if(A[i]>=i){
			ans=i;
			break;
		}
	}
	p(ans-1);
	return 0;
}

C++: //A Way to Invite Friends
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct Friend{
	int NMin, NMax;//人数最小最大
	bool operator<(const Friend& var) const{ return NMin < var.NMin; };
};

int main(){
	int FriendNum;//友人の数
	vector<Friend> Fri;//友人リスト
	int InviteFNum = 0;//招ける友人人数
	//代入
	cin >> FriendNum;
	Fri.reserve(FriendNum);
	{
		Friend temp;
		for (int i = 0; i < FriendNum; ++i){
			cin >> temp.NMin >> temp.NMax;
			//条件に一致し得ない友人を除外
			if (temp.NMin <= FriendNum + 1){
				Fri.push_back(temp);
			}
		}
	}

	//条件一致人数確認
	int CurFri = 0;
	for (int i = Fri.size(); i > 0; --i){
		//NMin <= i+1 <= NMaxである人数のカウント
		for (vector<Friend>::iterator it = Fri.begin(); it != Fri.end(); ++it){
			if (it->NMin <= (i + 1) && (i + 1) <= it->NMax)++CurFri;
		}
		//カウント人数(CurFri)が条件人数(i)以上ならok.->出力
		if (CurFri >= i){
			CurFri = i;
			break;
		} else{
			CurFri = 0;
		}
	}

	cout << CurFri << endl;
	return 0;
}