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 <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

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

int main(){
    int n; cin>>n;
    vector<int> imos(100010, 0);
    for(int i=0; i<n; i++){
        int a, b; cin>>a>>b;
        imos[a]++;
        imos[b+1]--;
    }

    for(int i=0; i<100008; i++){
        imos[i+1] += imos[i];
    }

    int ans=0;
    for(int i=1; i<100006; i++){
        if(i - 1 <= imos[i]){
            chmax(ans, i - 1);
        }
    }

    cout << ans << endl;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

signed main() {
	int n; cin >> n;
	vector<int>v(100010, 0);
	rep(i, 0, n) {
		int a, b; cin >> a >> b;
		for (int i = a; i <= b; i++)v[i]++;
	}
	int ans(0);
	rep(i, 0, v.size()) {
		if (v[i] == i)ans = i;
	}
	cout << max(0,ans-1) << endl;
	return 0;
}

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define INF 1LL << 61
#define MAX (1LL << 31) -1
#define MOD 1000000007

typedef long long ll;
typedef pair<int,int> pi;
//typedef pair<pair<int,int>,ll> p;
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
template<class T,class U>bool inline chmin(T&a,const U&b){if(a<=b)return false;a=b;return true;}
template<class T,class U>bool inline chmax(T&a,const U&b){if(a>=b)return false;a=b;return true;}
//__builtin_popcount(S);
//C.erase(unique(C.begin(),C.end()),C.end());
//#define int ll
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

int N;
vector<int> a,b;
int C[100010];

signed main(){
    cin>>N;
    a.resize(N); b.resize(N);
    for(int i=0;i<N;i++){
        cin>>a[i]>>b[i];
    }
    for(int i=0;i<N;i++){
        C[a[i]]++;
        C[b[i]+1]--;
    }
    for(int i=1;i<=N+1;i++){
        C[i]+=C[i-1];
    }
    int ans=0;
    for(int i=1;i<=N+1;i++){
        if(C[i]>=i-1) chmax(ans,i-1);
    }
    cout<<ans<<endl;
    
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
#include <ctype.h> // isdigit
#include <cassert>
using namespace std;

#define LE(n,m) ((n) < (m) + EPS * (abs(n) + abs(m)))
#define GE(n,m) ((n) + EPS * (abs(n) + abs(m)) > (m))
#define LT(n,m) ((n) + EPS * (abs(n) + abs(m)) < (m))
#define GT(n,m) ((n) > (m) + EPS * (abs(n) + abs(m)))
#define EQ(n,m) (abs((n)-(m)) < EPS * (abs(n) + abs(m)))

typedef long long int ll;
const int INF = (1<<30) - 1;
const double EPS = 1e-9;
const int MOD = 1000000007;

int N;
int table[100100];

//g++ -std=c++0x -msse4.2 -O3
//#include <bits/stdc++.h>
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout.precision(16);
    //cout.setf(ios::fixed);
    
    cin >> N;
    for(int i=0; i<N; i++){
        int a, b; cin >> a >> b;
        table[a]++;
        table[b+1]--;
    }
    table[0]++;
    table[100100-1]--;
    
    for(int i=1; i<100100; i++){
        table[i] += table[i-1];
    }
    
    int ans = 0;
    for(int i=0; i<100100; i++){
        if(i <= table[i])
            ans = i;
    }
    
    cout << ans-1 << endl;
    
    return 0;
}

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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
int main(){
  int n;cin>>n;
  vector<pair<int,int> >pii;
  int rui[100010]={};
  for(int i=0;i<n;i++){
    int a,b;cin>>a>>b;a-=2;b-=2;
    rui[a]++;
    rui[b]--;
  }
  int ma=0;
  int now=0;
  for(int i=0;i<n+1;i++){
    now+=rui[i];
    if(now>i)
    ma=max(ma,now);
  }
  cout<<ma<<endl;
  return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int count[100011] = {0}, ans = 0;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            count[a]++;
            count[b + 1]--;
        }
        rep(i,100005) count[i + 1]+=count[i];
        rep(i,100005){
            if(count[i] >= i && ans < i) ans = i;
        }
        cout << ans - 1 << endl;
    }
}

C++: #include <iostream>

#define REP(i,n) for(int i=0;i<n;i++)

using namespace std;


int main(){

	int n;
	int *a, *b;
	int *data;
	cin >> n;

	a = new int[n];
	b = new int[n];
	data = new int[100010];

	REP(i, n){
		cin >> a[i] >> b[i];
	}
	REP(i, 100010){
		data[i] = 0;
	}
	REP(i, n){
		data[a[i]]++;
		data[b[i]+1]--;
	}
	int num = 0;
	REP(i, 10){
	//	cout << data[i] << " ";
	}
	//cout << endl;
	REP(i, 10){
		num += data[i];
		data[i] = num;

		//cout << data[i] << " ";
	}
	//cout << endl;

	for (int i = n + 1; i > 0; i--){
		if (data[i] >= i){
			cout << i - 1 << endl;
			//cout << "Ohuotn" << endl;
			return 0;
		}
	}
	cout << 0 << endl;


	//cout << "Ohuotn" << endl;

	return 0;
}