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 <cstdio>
#include <cctype>
#include <utility>
#include <vector>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template<typename T>
#define _HT template<typename H,typename... T>
_T _DEF(T,in,int c){T n{};int m{1};while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_DEF(int,in,){return in<int>(gcu());}
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_T _SCAN(T &n){int c{gcu()};return c==EOF?false:(n=in<T>(c),true);}
_DEF(bool, scan, int &c){c=gcu();gcu();return c!=EOF;}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);}
_OUT(const char *s){while(*s)pcu(*s++);}
_OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_SCAN(string &s){int c;s{};for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=(char)c;}}
_OUT(string s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p{b};T m=n<0?pcu('-'),-1:1;if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
_HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();}
struct range{int e,b{0},s{1};range(int _b,int _e,int _s):e(_e),b(_b),s(_s){}range(int _b,int _e):e(_e),b(_b){}range(int _e):e(_e){}
	struct it{int v,s;it(int _v,int _s):v(_v),s(_s){}operator int()const{return v;}operator int&(){return v;}int operator*()const{return v;}it& operator++(){v+=s;return *this;}};
	it begin(){return{b, s};}it end(){return{e,s};}};

int main() {
	int n {in()}, m {n + 1}, r {}, t {};
	vector<int> q(m);
	for (int i {}; i < n; i++) {
		q[min(m, in() - 1)]++;
		q[min(m, in())]--;
	}
	for (int i: range(m))
		if ((t += q[i]) >= i)
			r = i;
	outl(r);
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e18
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> P;
const int mod = 1000000007;

int sum[100010];

signed main(){

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

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=1;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100000];
  lp(i,n+1){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+1){
    if(ans<a[i]&&a[i]<=i) ans=a[i];
  }
  cout<<ans-1<<endl;
  return 0;
    
}

C++: # define _CRT_SECURE_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <complex>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) begin(x),end(x)
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<":"<<x<<endl;
# define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
//const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

int main()
{
	INT(n);
	int a[100010] = {1};
	FOR(i, n)
	{
		INT(l); INT(r);
		a[l]++;
		a[r + 1]--;
	}
	int ans = 0;
	FORI(i, 1, 100010)
	{
		a[i] += a[i - 1];
		if (a[i] >= i)ans = i;
	}
	cout << ans-1 << endl;

}

C++: #include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
int c[100002];
int main() {
	int a, b, n, sum = 0;
	while (cin >> n) {
		int mab = 0;
		fill(c, c + 100002, 0);
		for (int z = 0; z < n; z++) {
			cin >> a >> b;
			for (int i = a; i <= b; i++) {
				c[i]++;
				mab = max(mab, c[i]);
			}
		}
		for (int i = mab; i >= 1; i--) {
			if (c[i] >= i) {
				cout << --i << endl;
				goto stop;
			}
		}
		cout << 0 << endl;
	stop:;
	}
}

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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)

using namespace std;

const int INF = 1<<29;

int x[100010];
int main(){
	int n;
	cin >> n;
	rep(i, n){
		int a, b;
		cin >> a >> b;
		x[a]++, x[b+1]--;
	}

	int num = 1;
	int best = 0;
	rep(i, 100010){
		num += x[i];
		if(num > i){
			best = max(best, i-1);
		}
	}
	cout << best << endl;

	return 0;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
#include <queue>

#define P pair<int, int>
using namespace std;

int N;
pair<int, int> ab[100005];
int end[100005] = {0};

priority_queue< P, vector< P >, greater< P > > Q;

int main(void)
{
	cin >> N;
	for(int i = 1; i <= N; i++){
		cin >> ab[i].first >> ab[i].second;
	}
	
	for(int i = 1; i <= N; i++){
		Q.push( ab[i] );
	}
	
	int num = 0;
	int max = 0;
	
	
	for(int i = 1; i <= 100005; i++){
		num -= end[i];
		while(Q.top().first == i && (int)Q.size()){
			num++;
			end[Q.top().second + 1]++;
			Q.pop();
		}
		
		if(num >= i-1) max = i;
		//if((int)Q.size() == 0) break;
	}
	
	end:
	cout << max-1 << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf =  1e9;
ll size = 100010;


int main(){
 ll n;
 cin>>n;
 ll res=0;
 vector<ll> c(size,0);
 for(ll i=0;i<n;i++){
  ll a,b;
  cin>>a>>b;
  c[a]++;
  c[b+1]--;
 }
 for(ll i=1;i<size;i++){
  c[i] += c[i-1];
 }
 for(ll i=0;i<size;i++){
  if( c[i] +1 >= i ) res = i-1;
 }
 cout<<res<<endl;

    return 0;
}