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 <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[100000];
int b[100000];

template<class T> T upper_search(T lb, T ub, bool (*C)(T)) {
  while(ub-lb>1) { // ツつアツつアツづ債適ツ督鳴づ可。
    T mid = (lb+ub)/2;
    if (C(mid)) lb = mid;       // ツ催渉ャツ値ツづーツ仰づ淞づゥツづ按づァlbツづbツづーツ禿シツづェツ妥鳴つヲツづゥ
    else ub = mid;
  }
  return lb;
}
int n;
bool C(int c) {
  int hoge = 0;
  REP(i, n) {
    if (a[i] <= c && c <= b[i]) hoge++;
    if (hoge == c) return 1;
  }
  return 0;
}

int main() {
  while(cin >> n) {
    REP(i,n) cin >> a[i] >> b[i];
    cout << max(0, upper_search(0, 100005, C)-1) << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

	int imos[100005];
	memset(imos, 0, sizeof(imos));

	rep(i, n) {
		int a, b;
		cin >> a >> b;

		imos[a]++;
		imos[b+1]--;
	}

	REP(i, 1, 100005) {
		imos[i] += imos[i-1];
	}

	int ans = 0;
	rep(i, 100005) {
		if(imos[i] >= i) {
			ans = i;
		}
	}

	cout << max(ans-1, 0) << endl;

	return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
using namespace std;

int main()
{
    int n, a, b, ans, max;
    ans = 0;
    max = 0;
    cin >> n;
    vector<int> p(100004, 0);
    vector<int> s(100004, 0);
    for(int i=0; i<n; ++i){
        cin >> a >> b;
        p[a]++;
        p[b+1]--;
    }
    for(int i=1; i<=n+1; ++i){
        if(i == 1){
            s[i] = 1;
        }
        else {
            s[i] = s[i-1] + p[i];
        }
        // cout << i << ":" << s[i] << endl;
    }
    for(int i=2; i<=n+1; ++i){
        if(i <= s[i]){
            ans = i - 1;
        }
    }
    cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAX = 1e6+2;

int main(){
	int n;
	cin >> n;
	
	vector<int> nf(MAX, 0);
	for(int i=0; i<n; i++){
		int a,b;
		cin >> a >> b;
		nf[a]++;
		nf[b]--;
	}
	
	int ans=0;
	for(int i=2; i<MAX; i++){
		nf[i] += nf[i-1];
		if(nf[i]+1>=i) ans=i-1;
	}
	cout << ans << endl;
	return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

int main(){

	int N,left,right,maximum;
	scanf("%d",&N);

	int* table = new int[100003];
	for(int i = 0; i <= 100002; i++){
		table[i] = 0;
	}

	for(int i = 0; i < N; i++){
		scanf("%d %d",&left,&right); //?´???????????¨???????????????????left???right???1????????????????????????
		table[left]++;
		table[right+1]--;
	}

	maximum = 0;
	for(int i = 1; i <= N+1; i++){
		table[i] += table[i-1];
		if(table[i]+1 >= i){ //?????¬???????????????????????§???????????§
			maximum = i; //i????????????????????°????????????????????§????????¬???????????????????????????i?????\????????????????????????
		}
	}

	printf("%d\n",min(N,max(maximum-1,0)));

	return 0;
}

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

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

//////
using pii = pair<int,int>;

bool isok(vector<pii> &ab, int n){
  int acc = 0;
  for(auto&& x:ab){
    if(x.first <= n && n <= x.second)acc++;
  }
  return acc>=n;
}

int main(){
  int n;
  cin >> n;
  vector<pii> ab(n);
  for(auto&& x:ab)cin >> x.first >> x.second;
  int res = 0;bool f = false;
  for(int i=1;i<=n;i++){
    if(isok(ab,i+1)){
      f = true;
      res = i;
    }
  }
  cout << res << 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,A.size()){
      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<vector>
#include<utility>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
#include<functional>
#include<tuple>
#include<iomanip>
#include<stack>
#include<cmath>
#define ll long long
using namespace std;
int main() {
	int n, data[100003] = {}; cin >> n;
	for (int i = 0; i < n; ++i) {
		int a, b; cin >> a >> b;
		data[a]++; data[b + 1]--;
	}
	int cnt = 0;
	for (int i = 2; i <= 100001; ++i) {
		data[i] += data[i - 1];
		if (data[i] >= i - 1) cnt = i;
	}
	cout << (cnt==0?cnt:cnt-1) << endl;
	return 0;
}

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

int main()
{
  int n;
  int num[100000][2];
  int maxa,minb;
  int i,j,k;
  int indexa,indexb;

  cin>>n;

  for(i=0;i<n;i++)
    cin>>num[i][0]>>num[i][1];

  for(k=0;k<n;k++){

    maxa=num[0][0];
    minb=num[0][1];
    indexa=indexb=0;
    for(i=1;i<n;i++){
      if(maxa<num[i][0]){
	maxa=num[i][0];
	indexa=i;
      }
      if(minb>num[i][1]){
	minb=num[i][1];
	indexb=i;
      }
    }
    if(maxa<=n-k && n-k<=minb){
      cout<<n-k-1<<endl;
      break;
    }else if(maxa<=n-k){
      num[indexb][0]=0;
      num[indexb][1]=100001;
    }else if(minb>=n-k){
      num[indexa][0]=0;
      num[indexa][1]=100001;
    }
  }
  if(k>n-2)
    cout<<"0"<<endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

const int INF = 1 << 24;
// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

// A[i] := i人で行きたい友達の人数
int A[100003];

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

    REP(i, N){
        int a, b;
        std::cin >> a >> b;

        A[a]++;
        A[b+1]--;
    }

    FOR(i, 1, (N+1)+1){
        A[i] += A[i-1];
    }

    // REP(i, (N+1)+1){
    //     std::cout << A[i] << std::endl;
    // }

    int res = 0;
    FOR(i, 1, (N+1)+1){
        if(i <= A[i]+1){res = i-1;}
    }

    std::cout << res << std::endl;
}