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 syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<ll,pll> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int n;
vi a;

int main(){
	cin>>n;
	a=vi(n+3);
	for(int i=0;i<n;i++){
		int l,r;
		cin>>l>>r;
		a[min(n+2,l)]++,a[min(n+2,r+1)]--;
	}
	for(int i=1;i<=n+2;i++) a[i]+=a[i-1];
	int res=0;
	for(int i=n+1;i>0;i--) if(a[i]>=i-1){
		res=i-1;
		break;
	}
	cout<<res<<endl;
}

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[100001] = {0}, ans = 0, sum = 0;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            a--;
            count[a]+= 1;
            count[b]-= 1;
        }
        rep(i,n + 1){
            sum+=count[i];
            if(sum >= i && i > ans) ans = i;
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>

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


using namespace std;

int main(){
	int n, a, b, fr[100005], ans=1;
	cin >> n;
	REP(i,100005){ fr[i] = 0;}
	fr[0] = 1;
	REP(i,n){
		cin >> a >> b;
		fr[a]++;
		fr[b+1]--;
	}
	REP(i,100004){
		fr[i+1] += fr[i];
	}
	for (int i=0; i<=n+1; i++){
		if (fr[i]>ans && fr[i]>=i){
			ans = min(i, fr[i]);
		}
	}
	
	
	//REP(i,10) cerr << fr[i] << " ";
	//cerr << endl;
	
	cout << ans-1 << endl;
	
}

C++: #include<stdio.h>
int N,F[100010],L,M,i,t=0,r=0;

int main()
{
	scanf("%d",&N);
	for(i=0;i<N;i++)
	{
		scanf("%d%d",&L,&M);
		F[L]++;
		F[M+1]--;
	}
	for(i=1;i<=N;i++)
	{
		t+=F[i];
		r=r>t?r:t;
	}
	printf("%d\n",r-1);
	return 0;
}

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T &a, const T &b) {return a = min(a, b);}
template <typename T> T &chmax(T &a, const T &b) {return a = max(a, b);}

using ll = long long;
using ld = long double;

const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);

int N;
using pii = pair<int, int>;
int num[100005];

int main(){
	cin >> N;
	vector<pii> req; 
	REP(cnt, N) {
		int a, b;
		cin >> a >> b;
		num[a]++; num[b+1]--;
	}
	num[0]++; num[100000]--;
	REP(i, N+2) num[i+1] += num[i];
	int res = 0;
	REP(i, N+2) if(num[i] >= i) res = max(res, i);
	cout << res-1 << endl;
	return 0;

}

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



int main() {
	int N;
	int num[100002];

	cin >> N;


	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		for (int j = a; j <= b && j <= N + 1; j++) {
			num[j]++;
		}
	}

	
	for (int i = N + 1; i > 0; i--) {
		if (num[i] >= i - 1) {
			cout << i - 1 << endl;
			break;
		}
	}



	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class BIT {
private:
	ll N;
	vector<ll> node;
public:
	BIT(ll M) {
		node = vector<ll>(M + 1, 0);
		N = M;
	}

	ll sum(ll i) {
		ll s = 0;
		while (i > 0) {
			s += node[i];
			i -= i & -i;
		}
		return s;
	}

	ll sum(ll i, ll j) {
		ll l = sum(i - 1);
		ll r = sum(j);
		return r - l;
	}

	void add(ll i, ll x) {
		while (i <= N) {
			node[i] += x;
			i += i & -i;
		}
	}
};


int main()
{
    ll N; scanf("%lld", &N);
	vector<vector<ll>> g(100050);
	BIT bit(105000);
    for (ll i = 0; i < N; i++) {
        ll a, b; scanf("%lld %lld", &a, &b);
		a--;
		b--;
		g[a].push_back(b);
    }
	ll res = 0;
	for (ll i = 1; i <= N; i++) {
		for (auto e : g[i]) {
			bit.add(e, 1);
		}
		ll sm = bit.sum(i, 100030);
		//cout << sm << endl;
		if (sm >= i) chmax(res, i);
	}
    cout << res << endl;

    return 0;

}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
  const int MAX = 100001;
  int n, a, b;
  int dp[MAX+1] = {};
   
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=2; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=2; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i]);
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include<cstdio>

int tbl[200000];

int main(void) {
    int n;
    scanf("%d",&n);

    tbl[1] = 1;

    for(int i=0; i<n; i++) {
        int a,b;
        scanf("%d%d",&a,&b);
        tbl[a]++; tbl[b+1]--;
    }

    for(int i=0; i<100500; i++)
        tbl[i+1] += tbl[i];

    int res=0;
    for(int i=0; i<100500; i++)
        if(tbl[i] >= i) res = i;
    printf("%d\n",res-1);
    
    return 0;
}

C++: #include <iostream>
using namespace std;
int n,i,a[100002],b,c,m,j;
int main(){
  for(cin>>n;i<n;i++){
    cin>>b>>c;
    for(j=b;j<=c;j++)a[j]++;
  }
  for(i=0;i<=n;i++)m=max(m,a[i]);
  cout<<m-1<<endl;
}