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 _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
 
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
 
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1,6);
uniform_real_distribution<double> score(0.0,10.0);

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

    vi sum(n + 3);
    rep(loop, n){
        int a, b; cin >> a >> b;
        chmin(a, n + 2);
        chmin(b, n + 1);
        sum[a]++, sum[b + 1]--;
    }

    int res = 0;
    rep(i, 1, n + 2){
        sum[i] += sum[i - 1];

        if(sum[i] + 1 >= i){
            chmax(res, i - 1);
        }
    }

    cout << res << endl;

    return 0;
}

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

int main() {
    int N;
    cin >> N;
    vector<int> V(N + 2);
    for (int i = 0; i < N; i++) {
        int a, b; cin >> a >> b;
        V[a]++;
        V[b+1]--;
    }

    int res = 0;
    for (int i = 0; i <= N; i++) {
        V[i + 1] += V[i];
        if (i <= V[i] + 1) res = i;
    }

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

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

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

C++: #include <iostream>
using namespace std;
const int MAX = 100002;

int main(){
  int n,ans[MAX],a,b;

  fill(ans,ans+MAX,0);

  cin >> n;
  while(n--){
    cin >> a >> b;
    ans[a]++; ans[b+1]--;
  }

  for(int i = 1 ; i < MAX ; i++)
    ans[i] += ans[i-1];

  int fri = 0;
  for(int i = 1 ; i < MAX ; i++){
    if(ans[i]+1 >= i) fri = i-1; 
  }

  cout << fri << endl;
  
  return 0;
}

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

#define MAX 100002

using namespace std;

int memo[MAX];

int main(void){

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

  return 0;
}

C++:  #include <iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define P pair<int,int>
#define mp make_pair


int t[100002];
int x[100002];
int y[100002];
int z[100002];
int n;
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
			for(int j=a-1;j<b;j++)t[j]++;
			y[a]++;
			z[b]--;
	}
	
	int ans=0;
	//sfor(int i=0;i<=n;i++)cout<<t[i]<<" ";cout<<endl;
	for(int i=0;i<=n;i++)if(t[i]==i+1)ans=i;//t[i]??\??????????????°i???????????°


	cout<<ans<<endl;
	return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;
int n,a[100005];

int main(){
  cin>>n;
  for(int i=0,s,t;i<n;i++)cin>>s>>t,a[s-1]++,a[t-1]--;
  int ans=0;
  for(int i=1;i<=n;i++){
    a[i]+=a[i-1];
    if(i<=a[i]) ans=max(i,ans);
  }
  cout <<ans<<endl;
  return 0;
}

C++: #include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
class bit{
private:
  vector<int> a;
public:
  bit(int n){
    a.assign(n,0);
  }
  int at(int p){
    int sm=0;
    for(;p;p&=p-1)
      sm+=a[p];
    return sm;
  }
  void update(int p,int q){  
    for(;p<(int)a.size();p+=p&-p)
      a[p]+=q;
  }
};
int main(){
  int i;
  int n;
  cin>>n;
  bit a(100002);
  for(i=0;i<n;++i){
    int p,q;
    cin>>p>>q;
    a.update(p,1);
    a.update(q+1,-1);
  }
  for(i=n+1;a.at(i)+1<i;--i);
  cout<<i-1<<endl;
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

    vector<int> x(100002);
    for(int i=0; i<n; ++i){
        int a, b;
        cin >> a >> b;
        ++ x[b];
        -- x[a-1];
    }

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

    return 0;
}

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

using namespace std;

typedef pair<int, int>P;

int N;

int main(){
  cin >> N;
  vector<P>V(N);
  for(auto &v: V)
    cin >> v.first >> v.second;

  sort(V.begin(), V.end());
  int res = 0;
  priority_queue<int, vector<int>, greater<int> >Q;
  for(auto &v: V){
    //    cout << v.first << " " << Q.size() << endl;
    if(v.first-2 <= (int)Q.size())
      Q.push(v.second);
    //    cout << Q.top() << " " << Q.size() << endl;    
    while(!Q.empty() && Q.top() <= (int)Q.size()){

      Q.pop();
    }
    res = max(res, (int)Q.size());
  }

  cout << res << endl;
}