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 mini = INF, maxi = 0;
    int num[100002] = {};
    rep(i, n) {
        int a, b;
        cin >> a >> b;
        minch(mini, a), maxch(maxi, b + 1);
        num[a]++, num[b + 1]--;
    }
    int ans = 0, cnt = 0;
    repl(i, mini, min(n + 1, maxi)) cnt += num[i], maxch(ans, cnt);
    cout << ans - 1 << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef pair<int,int> P;

int n;
int A = 0, B = INF;

int main(){
	cin >> n;
	rep(i,n){
		int a, b; cin >> a >> b;
		a--; b--;
		A = max(A,a);
		B = min(B,b);
	}
	int ans = 0;
	if(A > B || A > n) ans = 0;
	else ans = min(n,B);
	cout << ans << endl;
}

C++: /*
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331
 */

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;

int main() {
  int n;
  cin >> n;
  vi a(n), b(n);
  int maxn = 0;
  REP(i, n){
    cin >> a[i] >> b[i];
    maxn = max(b[i], maxn);
  }

  SORT(a);
  SORT(b);

  auto ita = a.begin();
  auto itb = b.begin();

  int res = 0;
  int cnt = 0;

  REP(i, maxn){
    while(*ita == i) {
      ita++;
      cnt++;
    }
    while(*itb == i - 1) {
      itb++;
      cnt--;
    }

    if (i - 1 < cnt) {
      res = max(res, i - 1);
    }
  }
  cout << res << endl;
}

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

#define REP(i ,n) for(int i=0 ;i < n; i++)
#define REPB(i ,n) for(int i=n; i >= 0; i--)
#define FOR(i ,m ,n) for(int i=m; i < n; i++)
#define FORB(i, m, n) for(int i=m; i>= n; i--)
#define ll long long
#define pb push_back
#define popb pop_back
#define debug(i) cout << i << endl
using namespace std;
int people[100003] = {};

int main(){
  REP(i, 100003) people[i]++;

  int n;
  cin >> n;

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

      FOR(j,a,(b+1)) people[j]++;
    }

    REPB(i, 100001){
        if(people[i] >= i){
            cout << (i - 1) << endl;
            return 0;
          }
      }
}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int imos[1001000]={0};
    int gotiusa; cin>>gotiusa;
    vector<pii>v;
    rep(i,gotiusa){
        int tiya,rize; cin>>tiya>>rize;
        imos[tiya]++; imos[rize+1]--;
        v.push_back(pii(tiya,rize));
    }
    rep(_,1001000)
    if(_)imos[_]+=imos[_-1];
    int ans=0;
    rep(i,1001000){
        if(imos[i]>=i-1)cmax(ans,i-1);
    }
    cout<<ans<<endl;
    
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
typedef pair<int, int> P;

long long n, a[100010], b[100010], ma=0, mi=100010;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
	}
	for (int i = 0; i < n; i++) {
		if (a[i] > ma)ma = a[i];
		if (b[i] < mi)mi = b[i];
	}
	if (mi - ma<0 || ma>n + 1)cout << 0 << endl;
	else if(n+1<mi){
		cout << n << endl;
	}
	else {
		cout << mi - 1 << endl;
	}
	return 0;
}

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define INF 1LL << 61
#define MAX (1LL << 31) -1
#define MOD 1000000007

typedef long long ll;
typedef pair<int,int> pi;
//typedef pair<pair<int,int>,ll> p;
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
template<class T,class U>bool inline chmin(T&a,const U&b){if(a<=b)return false;a=b;return true;}
template<class T,class U>bool inline chmax(T&a,const U&b){if(a>=b)return false;a=b;return true;}
//__builtin_popcount(S);
//C.erase(unique(C.begin(),C.end()),C.end());
//#define int ll
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

int N;
vector<int> a,b;
int C[100010];

signed main(){
    cin>>N;
    a.resize(N); b.resize(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<=N+1;i++){
        C[i]+=C[i-1];
    }
    int ans=0;
    for(int i=1;i<=N+1;i++){
        if(C[i]>=i-1) chmax(ans,i-1);
    }
    cout<<ans<<endl;
    
}