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;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define repr(i,n) for(int i=(int)(n-1);i>=0;i--)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout << #x" = " << x << endl
#define print(x) cout << x << endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX/3
#define MAX_N 1000000001


int main(){
    cin.sync_with_stdio(false);
    int n;
    cin >> n;
    int num[100002] = {};
    num[1] = 1;
    rep(i, n) {
        int a, b;
        cin >> a >> b;
        num[a]++, num[b + 1]--;
    }
    int ans = 1;
    repl(i, 2, n + 2) {
        num[i] += num[i - 1];
        if (i <= num[i]) ans = i;
    }
    cout << ans - 1 << endl;

    return 0;
}

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

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

struct FenwickTree{
	vector<ll> data;
	FenwickTree(int n):data(n+1){}
	void AddRange(int i,int x){
		for(;i;i-=i&-i)
			data[i]+=x;
	}
	void AddRange(int i,int j,int x){
		AddRange(i,-x);
		AddRange(j,x);
	}
	int Get(int i){
		ll res=0;
		for(i++;i<data.size();i+=i&-i)
			res+=data[i];
		return res;
	}
};

int main()
{
	for(int n;cin>>n && n;){
		FenwickTree ft(n+1);
		rep(i,n){
			int a,b; cin>>a>>b; a--;
			a=min(a,n+1);
			b=min(b,n+1);
			ft.AddRange(a,b,1);
		}
		int res=0;
		repi(i,1,n+1)
			if(ft.Get(i)>=i)
				res=i;
		cout<<res<<endl;
	}
}

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

int main() {
    int n, a, b;
    int p[100002] = {};
    int sum = 1;
    int res = 0;
    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> a >> b;
        p[a]++;
        p[b+1]--;
    }

    for (int i=0; i<n; i++) {
        sum += p[i];
        if (i <= sum) {
            res = i;
        }
    }

    cout << res << endl;
    return 0;
}

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

using namespace std;

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

	return 0;
}

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

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    c[a]++;
    c[b]--;
  }
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  for(int i=0;i<10;i++){
    cout << c[i] << endl;
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i == c[i]+1) maxnum = i;
  }
  cout << maxnum << endl;
  return 0;
}

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 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 <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> imos(1e5 + 3);
  while (n--) {
    int a, b;
    cin >> a >> b;
    imos[a] += 1;
    imos[b + 1] -= 1;
  }
  int res = 0;
  for (int i = 0; i + 1 < (int)imos.size(); ++i) {
    imos[i + 1] += imos[i];
    if (imos[i] + 1 >= i) {
      res = max(res, i - 1);
    }
  }
  cout << res << '\n';
}

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

int main() {
  int N, a, b, res;
  signed int invite[100000] = {0};

  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> a >> b;
    invite[a-1]++;
    invite[b]--;
  }

  signed int sum = 0;
  for(int i = 0; i < N; i++) {
    sum += invite[i];
    if(i <= sum)
      res = i;
  }
  cout << res << endl;
}