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 <fstream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

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

int solve() {
	int cnt = 1;
	int ret = 0;
	for(int i = 1; i <= 100000; i++) {
		cnt += imos[i];
		if(cnt >= i) {
			ret = i - 1;
		}
	}
	return ret;
}

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

	while(cin >> n) {
		memset(imos, 0, sizeof imos);
		for(int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
			imos[a[i]]++;
			imos[b[i] + 1]--;
		}
		cout << solve() << endl;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;char c;char str[1024];string s;ll l;
	int nextInt(){scanf("%d", &n);return n;}
	ll nextLong(){scanf("%lld", &l);return l;}
	char nextChar(){scanf("%c", &c);return c;}
	string next(){scanf("%s", str);return string(str);}
	string nextLine(){getline(cin, s);return s;}
};
Input in;

int c[100100];

int main(){
	int n = in.nextInt();
	vi a(n), b(n);
	rep(i, n) a[i] = in.nextInt(), b[i] = in.nextInt();

	vector<pii> p;
	rep(i, n) p.pb({a[i], b[i]});
	sort(all(p));

	int ans = 0;

	int idx1 = 0, idx2 = 0;
	for(int i = 0; i < n + 1; i++){
		//i + 1 nin de ikeru tomodachi no kazu
		int idx = upper_bound(all(p), mp(i + 1, INF)) - p.begin();
		//c[x] := x nin ijou ha dame
		for(int j = idx1; j < idx; j++){
			if(p[j].se < i) c[i]++;
			c[p[j].se]++;
		}
		if(i > 0) c[i] += c[i-1];
		// i + 1 nin atumaranai
		if(i > idx) continue;
		ans = max(ans, min(i, idx - c[i]));
	}

	cout<<ans<<endl;
}

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

#define rep(i,x,y) for(int (i)=(x);i<(y);(i)++)
#define INF 1000000000

vector<int> A;
vector<int> B;

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

  rep(i,0,N){
    int a,b;
    cin>>a>>b;
    A.push_back(a);
    B.push_back(b);
  }

  int ans=0;
  rep(i,0,N+1){
    bool flag=false;
    int count=0;
    rep(j,0,N){
      if(A[j]<=i+1&&i+1<=B[j]){
        count++;
      }
      if(count>=i){
        flag=true;
        break;
      }
    }
    if(flag==true){
      ans=i;
    }
  }

  cout<<ans<<endl;

  return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

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

C++: #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);++i)
#define rep(i,n) REP(i, 0, n)
typedef long long int i64;
typedef pair<int, int> pint;
int imos[100010];
int main() {
    int N; scanf("%d", &N);
    int a, b;
    memset(imos, 0, sizeof(imos));
    rep(i, N) {
        scanf("%d%d", &a, &b);
        imos[a]++; imos[b + 1]--;
    }
    rep(i, N + 2) imos[i + 1] += imos[i];
    rep(i, N + 2) {
        if(imos[i] < i - 1) imos[i] = 0;
        if(imos[i] > i - 1) imos[i] = i - 1;
    }
    int ans = 0;
    rep(i, N + 2) ans = max(ans, imos[i]);
    printf("%d\n", ans);
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
 
int main(){
    int n;
    cin>>n;
    int a[n],b[n];
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i];
    }
    for(int num=n+1;num>=0;num--){
        int cnt=0;
        for(int i=0;i<n;i++){
            if(num>=a[i] && num<=b[i])cnt++;
        }
        if(cnt>=num-1){
            cout<<num-1<<endl;
            return 0;
        }
    }
}

C++: #include<iostream>

using namespace std;

int c[100100];

int main(){

    int N;
    cin>> N;
    int a[N], b[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<100100; i++){
        c[i]+=c[i-1];
    }
    int s=0;
    for(int i=0; i+1<100100; i++){
        if(c[i+1]>=i){
            s=max(s, i);
        }
    }
    cout<< s<< endl;

    return 0;
}

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

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


int imos[100010];
void solve() {
    int N;cin>>N;
    REP(i,N){
        int l,r;cin>>l>>r;
        l--;
        imos[l]++;
        imos[r]--;
    }
    REP(i,100009){
        imos[i+1]+=imos[i];
    }
    int ans = 0;
    REP(i,100010){
        if(imos[i]>=i)ans=i;
    }
    cout<<ans<<endl;
}

signed main() {
	solve();
}

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 cout<<1<<endl;
	return 0;
}

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

int a[100000] = {};

int main(void)
{
	int max;
	cin >> max;
	a[0]++;
	for(int i=0;i<max;++i){
		int c,b;
		cin >> c >> b;
		a[c]++;a[b+1]--;
	}
	int sum=0;
	int maxn=-1;
	for(int i=0;i<=max;++i){
		sum+=a[i];
		if(i < sum)
		{
			if(maxn<sum)
				maxn = sum;
		}
	}
	cout << maxn-1 << endl;
	return 0;
}

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

using namespace std;
typedef long long ll;

int N;
int ruisekiwa[100000 + 10];

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		ruisekiwa[a]++, ruisekiwa[b + 1]--;
	}
	for (int i = 1; i <= 100005; i++) {
		ruisekiwa[i] += ruisekiwa[i - 1];
	}
	int ans = 1;
	for (int i = 0; i <= 100005; i++) {
		if (i - 1 <= ruisekiwa[i])ans = max(ans, i);
	}
	cout << ans - 1 << endl;
	return 0;
}