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++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

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

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {return (!n)?1:(mod_pow((x*x)%MOD,n/2)*((n&1)?x:1))%MOD;}
int madd(int a, int b) {return (a + b) % MOD;}
int msub(int a, int b) {return (a - b + MOD) % MOD;}
int mmul(int a, int b) {return (a * b) % MOD;}
int minv(int a) {return mod_pow(a, MOD-2);}
int mdiv(int a, int b) {return mmul(a, minv(b));}

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int imos[100010];

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

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

using namespace std;

int main(){

  int n;

  cin >> n;

  vector<int> a(n);
  vector<int> b(n);
  vector<int> num(1000000,0);
  int min = 1000000;
  
  for(int i = 0;i < n;++i){
	cin >> a[i] >> b[i];
	if(min > a[i])
	  min = a[i];
	for(int j = a[i];j <= b[i];++j)
	  num[j]++;
  }

  int max_fri = 0;
  
  for(int i = min;i <= n + 1;++i){
	for(int j = 0;j < n;++j){
	  if(i <= num[i] + 1 & max_fri < i - 1)
		max_fri = i - 1;
	}
  }
  
  cout << max_fri << endl;
  
}

C++: #include <bits/stdc++.h>
using namespace std;
// typename alias
template <class T> using vec = vector<T>;
template <class T> using vvec = vec<vec<T> >;
template <class T, class U> using vep = vec<pair<T,U> >;
template <class T> using PQ = priority_queue<T>;
using ll = long long;
using vll = vec<ll>;
using vvll = vvec<ll>;
using pll = pair<ll, ll>;
using vpll = vec<pll>;
using str = string;
// きけん
//#define int ll
#define bits(x) (1LL << (x))

// const
static const long long INF = 1LL<<59;
static const int iINF = 1LL<<30;
static const double dINF = 1e290;
static const long long MOD = (ll)1e9+7;
static const double PI = 3.14159265358979323846;

// accelerate stdin, stdout
#define endl "\n"
#define presolve(n) ios_base::sync_with_stdio(false);cin.tie(NULL);cout.setf(ios::fixed);cout.precision(n);
// repeat macro
#define __ov3(_1, _2, _3, name, ...) name
#define REP(i, n) for(ll (i)=0;(i)<(n);(i)++)
#define FOR(i, a, b) for(ll (i)=(a);(i)<=(b);i++)
#define rFOR(i, a, b) for(ll (i)=(b);(i)>=(a);i--)
#define rep(...) __ov3(__VA_ARGS__, FOR, REP,)(__VA_ARGS__)
// vector macro
#define rng(v) (v).begin(), (v).end()
#define ASC(v) sort(rng(v))
#define DESC(v) sort((v).rbegin(), (v).rend())
#define INV(v) reverse(rng(v))
#define FOLDL(src, dst, lambda) partial_sum(rng(src), (dst).begin(), lambda)
#define FOLDR(src, dst, lambda) INV(src);partial_sum(rng(src), (dst).begin(), lambda);INV(dst)
#define CUMSUML(src, dst) FOLDL(src, dst, [](auto a, auto b) {return a+b;})
#define CUMSUMR(src, dst) FOLDR(src, dst, [](auto a, auto b) {return a+b;})
#define ACCUM(v, d0, lambda) accumulate(rng(v), d0, lambda)
#define SUM(v, d0) ACCUM(v, d0, [](auto a, auto b) {return a+b;})
#define ERASE(v, i) v.erase(v.begin() + i)
#define UNIQ(v) ASC(v);v.erase(unique(v.begin(), v.end()), v.end())
template <typename T> T min(vec<T> v) {return *min_element(rng(v));}
template <typename T> T max(vec<T> v) {return *max_element(rng(v));}

template <typename T> inline bool chmin(T& a, const T& b) {if(a > b) {a = b;return 1;}return 0;}  
template <typename T> inline bool chmax(T& a, const T& b) {if(a < b) {a = b;return 1;}return 0;}


// io helper
template <typename T>void debug(T t) {cerr << t << endl;}
template <typename T, typename ...Args>void debug(T t, Args ...args) {cerr << t << " ";debug(args...);}
template <typename T>void out(T t) {cout << t << endl;}
template <typename T, typename ...Args>void out(T t, Args ...args) {cout << t << " ";out(args...);}
template <typename T>istream& operator>>(istream &is, vec<T> &v) {for(auto &a:v) is >> a; return is;}
template <typename T>ostream& operator<<(ostream &os, vec<T> &v) {os << "[";REP(i, v.size() - 1) os << v[i] <<", "; os << v[v.size()-1]<<"]";return os;}
template <typename T, typename U>ostream& operator<<(ostream &os, pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")";return os;}
template <size_t N, typename T>ostream& operator<<(ostream &os, array<T, N> &v) {os << "[";REP(i, N - 1) os << v[i] <<", "; os << v[N-1]<<"]";return os;}
template <typename T, typename U>ostream& operator<<(ostream &os, map<T, U> &mp) {os << "{ ";for(auto &p:mp) os << p.first << ": " << p.second << ", "; os << "}";return os;}

void solve() {
    ll N;
    cin >> N;
    vll A(N), B(N);
    rep(i, N) cin >> A[i] >> B[i];
    vll imos(100010, 0), I(100010, 0);
    rep(i, N) {
        I[A[i]]++;
    }
    rep(i, N) {
        I[B[i]+1]--;
    }
    ll a = -INF, r = -1;
    CUMSUML(I, imos);
    ll ans = -1;
    rep(i, 100010) {
        if(imos[i] + 1 >= i) {
            ans = i;
        }
    }
    out(max(0LL, ans - 1));
}

signed main() {
    presolve(20);
    solve();
    return 0;
}