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<bits/stdc++.h>

#define REP(i,a) for(long i=0;i<(a);++i)

typedef std::vector<int>  VI;

int N;
VI imos;
int main() {
    std::cin >> N;
    imos.resize(N+5, 0);

    REP(i, N) {
        int a, b;
        std::cin >> a >> b;
        ++imos[a];
        --imos[b];
    }

    int x = 0, ans = 0;
    for(int i=0; i<=N+1; ++i) {
        x += imos[i];
        if(x + 1 >= i) {
            ans = i - 1;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

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=-1;
	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+1){
		if(ans<dt[i])ans=dt[i];
	}
	if(n!=1)cout<<ans-1<<endl;
	else if(dt[1]!=0)cout<<1<<endl;
	else cout<<0<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int main()
{
    int n;
    cin >>n;

    int r[100002]={0};
    rep(i,n)
    {
        int a,b;
        scanf(" %d %d", &a, &b);
        ++r[a];
        --r[b+1];
    }

    rep(i,100001) r[i+1]+=r[i];

    int ans=n;
    while(ans>0 && r[ans+1]!=n) --ans;

    printf("%d\n", ans);
    return 0;
}

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

using namespace std;

pair<int,int> friends[100000];
int main(){
  int N;
  cin >> N;
  for(int i=0;i<N;++i){
    cin >> friends[i].first >> friends[i].second;
  }

  int max_num = 1;
  for(int i=2;i<=100001;++i){
    int num = 1;
    for(int j=0;j<N;++j){
      if(friends[j].first<=i&&i<=friends[j].second){
        num++ ;
      }
    }
    if(num >=i) max_num = i;
  }
  cout << max_num-1 << endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  std::cin >> n;
  vector<int> f(1e6 + 1, 0);
  int a, b, ans = 1;
  for (int i = 0; i < n; i++) {
    std::cin >> a >> b;
    f[a]++;
    f[b + 1]--;
  }
  for (int i = 1; i <= 1e6; i++)f[i] += f[i - 1];
  for (int i = 0; i <= 1e6; i++) {
    ans = max(ans, (i <= f[i])?i:1);
  }
  std::cout << ans - 1 << std::endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1<<30;
const ll mod = 10000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

//#define int long long
#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod

int n;
int a[100000 + 5], b[100000 + 5];
int imos[100000 + 5];

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    rep(i, n) {
        cin >> a[i] >> b[i];
        imos[a[i]]++;
        imos[b[i] + 1]--;
    }
    rep(i, 100000 + 3) imos[i + 1] += imos[i];
    int ans = 0;
    rep(i, 100000 + 5) {
        if (i == imos[i]) ans = max(ans, imos[i] - 1);
    }
    cout << ans << endl;
}

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>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {return (!n)?1:(mod_pow((x*x)%MOD,n/2)*((n&1)?x:1))%MOD;}
int madd(int a, int b) {return (a + b) % MOD;}
int msub(int a, int b) {return (a - b + MOD) % MOD;}
int mmul(int a, int b) {return (a * b) % MOD;}
int minv(int a) {return mod_pow(a, MOD-2);}
int mdiv(int a, int b) {return mmul(a, minv(b));}

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int imos[100010];

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

C++: #include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int data[N];

int main(){
	int n;
	cin >> n;

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[j]++;
	}

	//debug
	//for(int i = 0; i < 10; i++) cout << "arr = " << data[i] << endl;

	int ans = 0;
	vector<int> v;
	for(int i = 1; i <= N; i++){
		int ok = data[i];
		if(ok + 1 >= i) ans = max(ans, i);
	}

	cout << ans - 1 << endl;

	return 0;
}