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 <complex>
#include <vector>
#include <unordered_map> 
#include <algorithm>

/*
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=jp
 * test of c++
 */


template <typename T>
std::vector<std::vector<T>> read_input() {
    std::string input_str;

    std::getline(std::cin, input_str);
    int arr_size = std::stoi(input_str);
    std::vector<std::vector<T>> input_vec(arr_size, std::vector<int>(2, 0));

    for (size_t i = 0; i < arr_size; i++) {
        std::getline(std::cin, input_str);
        std::istringstream iss(input_str);
        T begin, end;
        iss >> begin >> end;

        // this is for myself
        begin -= 1;
        end -= 1;
        input_vec.at(i).at(0) = begin;
        input_vec.at(i).at(1) = end;
    }
    return input_vec;
}


// bool verify(std::vector<std::vector<int>> input_vec, int ans) {
//     if (ans == 0) {
//         return true;
//     }
//     int count = 0;
//     for (int i = 0; i < input_vec.size(); i++) {
//         std::vector<int> person = input_vec.at(i);
//         if (person.at(0) <= ans && ans <= person.at(1)) {
//             count++;
//         }
//         if (count == ans) {
//             return true;
//         }
//     }
//     return false;
// }


// void search(std::vector<std::vector<int>> input_vec, int bottom, int top, int* ans) {
//     if (bottom >= top) {
//         if (*ans > bottom) {
//             return;
//         }
//         else {
//             if (verify(input_vec, bottom)) {
//                 *ans = bottom;
//             }
//             return;
//         }
//     }
//     int num = bottom + (top - bottom) / 2;
//     if (verify(input_vec, num)) {
//         *ans = num;
//         search(input_vec, num+1, top, ans);
//     }
//     else {
//         search(input_vec, num+1, top, ans);
//         search(input_vec, bottom, num-1, ans);
//     }
    // naive
    // int ans;
    // for (int num = 0; num <= size; num++) {
    //     if (verify(input_vec, num)) {
    //         ans = num;
    //     }
    // }
    // return ans;
// }


int main(int argc, char const *argv[]) {
    std::vector<std::vector<int>> input_vec = read_input<int>();
    // search(input_vec, 0, input_vec.size(), &ans);

    // counts[j] people can endure j+1 people.
    std::vector<int> counts(input_vec.size(), 0);
    for (int i = 0; i < input_vec.size(); i++) {
        std::vector<int> person = input_vec.at(i);
        for (int j = person.at(0); j <= person.at(1) && j <= counts.size(); j++) {
            counts.at(j-1) += 1;
        }
    }
    // for (int i = 0; i < counts.size(); i++) {
    //     std::cout << counts.at(i);
    // }
    // std::cout << std::endl;

    int ans = 0;
    for (int i = 0; i < counts.size(); i++) {
        if (counts.at(i) > i) {
            ans = i + 1;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int a[100000], b[100000];
int main() {
	int n; scanf("%d", &n);
	rep(i, n)scanf("%d%d", &a[i], &b[i]);
	int s = 0, g = n;
	while (1) {
		int i = (s + g) / 2;
		if (g - s == 1)i = g;
		int cnt = 0;
		for (int j = 0; j < n&&cnt < i; j++) {
			if (a[j] - 1 <= i&&i < b[j])cnt++;
		}
		if (cnt == i)s = i;
		else g = i - 1;
		if (s == g) {
			printf("%d\n", s);
			return 0;
		}
	}
}

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

#define REP(i, n, N) for(ll i=(n); i<(N); i++)
#define RREP(i, n, N) for(ll i=(N-1); i>=n; i--)
#define SPACEREP(lst,itr) for(auto itr=lst.begin(); itr!=lst.end();++itr){cout<<*itr<<(++itr!=anslist.end()?" ":"\n");itr--;}
#define CK(n, a, b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define MCP(a, b) memcpy(b,a,sizeof(b))
#define p(s) cout<<(s)<<endl
#define p2(a, b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define LEN 100001

int main(){
    int n,a,b,ans=0;
    cin >> n;
    vector<int> ary(LEN+1);
    REP(i,0,LEN+1){
        ary[i]=0;
    }
    REP(i,0,n){
        cin >> a;
        cin >> b;
        ary[a]+=1;
        ary[b+1]+=-1;
    }
    /**
    REP(i,1,LEN+1){
        cout << ary[i] <<" ";
    }
    
    cout << endl;
    **/
    REP(i,1,LEN+1){
        ary[i]+=ary[i-1];
    }
    /**
    REP(i,1,LEN+1){
        cout << ary[i] << " ";
    }
    cout << endl;
    **/

    RREP(i,1,LEN+1){
        if(ary[i] >= i-1){
            ans=i-1;
            break;
        }
    }
    p(ans);

}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


signed main() {
	cin1(n);

	vpii p(n);
	for (int i = 0; i < n; i++) {
		cin2(p[i].fr,p[i].sc);	
	}

	sort(p.begin(),p.end());
	reverse(p.begin(),p.end());

	int ans = n+1;
	for (int i = 0; i < n; i++) {
		//cout << p[i].fr << " " << p[i].sc << endl;	
		if (p[i].fr > ans) {
			ans--;
		}else if(p[i].sc < ans) {
			ans--;
		}
	}
	cout(ans-1);
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

int num[100005];
int num2[100005];

int main(){
    int N,a,b;
    cin>>N;
    for(int n=0;n<N;n++){
        cin>>a>>b;
        // cout<<a<<' '<<b<<endl;
        num[a]++;
        num[b+1]--;
    }
    int ans=num2[0]=num[0];
    for(int i=1;i<100005;i++){
        num2[i]=num2[i-1]+num[i];
        if(num2[i]>=i-1) ans=i;
    }
    ans--;
    cout<<max(0,ans)<<endl;
}

C++: #include <iostream>
using namespace std;

int main() {
  int N, a, b, res;
  signed int invite[100000] = {0};

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

  signed int sum = 0;
  for(int i = 0; i < 100000; i++) {
    sum += invite[i];
    if(i <= sum)
      res = i;
  }
  cout << res << endl;
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int n,a,b,ans=0;
	cin>>n;
	int dt[100002]={};
	rep(i,n){
		cin>>a>>b;
		dt[a]++;
		dt[b+1]--;
	}
	int cnt=-1;
	loop(i,1,100002){
		dt[i]+=dt[i-1];
	}
	rep(i,n+2){
		if(dt[i]>=i-1)ans=i;
	}
	cout<<ans-1<<endl;
	return 0;
}