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>
 
using namespace std;
 
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
 
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
 
//typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
 
//repetition
//------------------------------------------
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
 
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INF=INT_MAX;
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const int MAX_SIZE=400007;
LL segMax[MAX_SIZE - 1], segAdd[MAX_SIZE - 1];
int n;

//??????[a, b)??????x???????????????.
void add(int a, int b, int x, int k = 0, int l = 0, int r = n)
{
	if (r <= a || b <= l) return;
	
	if (a <= l && r <= b){
		segAdd[k] += x;
		while (k){
			k = (k - 1) / 2;
			segMax[k] = max(segMax[k * 2 + 1] + segAdd[k * 2 + 1], segMax[k * 2 + 2] + segAdd[k * 2 + 2]);
		}
		return;
	}
	
	add(a, b, x, k * 2 + 1, l, (l + r) / 2); 
	add(a, b, x, k * 2 + 2, (l + r) / 2, r); 
}

LL getMax(int a, int b, int k = 0, int l = 0, int r = n)
{
	if (r <= a || b <= l) return 0LL;
	
	if (a <= l && r <= b) return (segMax[k] + segAdd[k]); 
	
	LL left = getMax(a, b, k * 2 + 1, l, (l + r) / 2);
	LL right = getMax(a, b, k * 2 + 2, (l + r) / 2, r); 
	
	return (max(left, right) + segAdd[k]);
	
}
int main(){
	CLR(segMax);CLR(segAdd);
	int N;
	cin>>N;
	n=100003;
	REP(i,N){
		int l,r;
		cin>>l>>r;
		add(l,r+1,1);
	}
	int res=0;
	REP(i,100002){
		if(getMax(i,i+1)+1>=i){
			res=i;
		}
		//if(i<=10)cout<<i<<" "<<getMax(i,i+1)<<endl;
	}
	
	cout<<res-1<<endl;
	return 0;
}

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





int main(){
  int n;
  cin>>n;
  int dp[100010]={};
  
  for(int i=0;i<n;i++){
    int a,b;
    cin>>a;
    cin>>b;
    if(n<b){b=n;}
    for(int t=a;t<=b;i++){dp[t]++;}
  }
  
  int mx=0;
  for(int i=n;i>0;i--){
    if(dp[i]>=i){mx=i; break;}
  }
  cout<<mx<<endl;
  
    
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-8;
const long long inf=1e16;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main(){
	int n;
	cin>>n;
	int dp[100100]={0};
	rep(i,n){
		int a,b;
		scanf("%d %d",&a,&b);
		dp[a]++;dp[b+1]--;
	}
	int ma=0;
	rep(i,100099)dp[i+1]+=dp[i];
	rep(i,100099){
		if(i-1<=dp[i])ma=max(ma,i-1);
	}
	cout<<ma<<endl;
}

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

using namespace std;


int main()
{
	int n,max=0;
	pair<int,int> a[110000];
	int v[110000]={};
	
	cin >> n;
	
	
	for(int i=0;i<n;i++){
		cin >> a[i].first >> a[i].second;
	}
	
	for(int i=0;i<n;i++){
		v[a[i].first]++;
		v[a[i].second+1]--;
	}
	v[0]++;
	for(int i=1;i<n+2;i++){
		v[i] = v[i]+v[i-1];
		if(v[i] >= i)max = i;
	}
	
	cout << max-1 << endl;
	
	
	
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

int n;
int a[100000], b[100000];

int solve() {
	int ok = 0, ng = 100001;
	while(abs(ok - ng) > 1) {
		int m = (ok + ng) / 2;
		int cnt = 0;
		for(int i = 0; i < n; i++) {
			if(a[i] <= m + 1 && m + 1 <= b[i]) cnt++;
		}
		if(cnt >= m) ok = m;
		else ng = m;
	}
	return ok;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(cin >> n) {
		for(int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
		}
		cout << solve() << endl;
	}
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

int a[MAX_N],b[MAX_N];
int n;

bool possible(int u)
{
    int cnt = 0;
    if(u == 0){
        return true;
    }
    rep(i,n){
        if(a[i] <= u + 1 && b[i] >= u + 1){
            cnt++;
            if(cnt >= u){
                return true;
            }
        }
    }
    return false;
}

int main()
{
	scanf("%d",&n);
	rep(i,n){
		scanf("%d%d",&a[i],&b[i]);
	}
    int res = -1;
    rep(i,n+1){
        if(possible(i) && i > res){
            res = i;
        }
    }
    cout << res << "\n";
	return 0;
}

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

using namespace std;

typedef pair<int,int> pii;
typedef vector<int> vi;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999



int main(){
    int n;
    cin>>n;
    int memo[200000]={};
    rep(i,n){
        int a,b;
        cin>>a>>b;
        memo[a]++;
        memo[b]--;
    }
    int ans=0;
    rep(i,190000){
        memo[i+1]=memo[i+1]+memo[i];
//        cout<<memo[i+1]+1<<" "<<i+1<<endl;
        if(memo[i+1]+1>=i+1){
            int x=min(memo[i+1],i+1);
            ans=max(ans,x);
//        cout<<ans<<endl;
        }
    }
//    rep(i,20)cout<<memo[i]<<" ";
    cout<<ans<<endl;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 10000000
using namespace std;
typedef pair<int,int> P;
int main(){
    vector<P> allow;
    int n;
    cin >> n;
    int tmp_min = INF;
    rep(i,n){
        int a,b;
        cin >> a >> b;
        a--; //b--;
        tmp_min = min(tmp_min,min(a,b));
        allow.push_back(P(a,b));
    }
    int tmp_max = 0;
    int ans = 0;
    for(int i=tmp_min; i<=n+1; i++){
        int cnt = 0;
        rep(j,n){
            if(i >= allow[j].first && i<= allow[j].second) cnt++;
            //cout << cnt << endl;
        }
        if(tmp_max <= cnt && i <= cnt) {
            tmp_max = cnt;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;
int n, a, b;
int cnt[108000];
int main(){
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> a >> b;
		cnt[a]++;
		cnt[b+1]--;
	}
	for(int i = 1;i < 108000;i++){
		cnt[i] += cnt[i-1];
	}
	for(int i = 107000;i >= 0;i--){
		if(cnt[i+1] >= i){
			cout << i << endl;
			return 0;
		}
	}
	return 0;
}