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 FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;


int main() {
    cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;

	vector<int> dp(100100, 0);
	FOR(i, 0, N) {
	    int a, b;
	    cin >> a >> b;
	    dp[a] += 1;
	    dp[b + 1] -= 1;
	}

	FOR(i, 0, dp.size() - 1) {
	    dp[i + 1] += dp[i];
	}

	int ans = 0;
	FOR(i, 0, dp.size()) {
	    if (dp[i] == i) {
            ans = max(ans, i - 1);
        }
	}
	print(ans);

	return 0;
}

C++: #include<iostream>
int N,a,b,v[1<<17],z,s;main(){std::cin>>N;while(N--)std::cin>>a>>b,v[a]++,v[b+1]--;while(++N<1<<17)z+=v[N],s=N<z+2?N-1:s;std::cout<<s<<'\n';}

C++: #include <iostream>
#include <string>
#include <array>
#include <vector>
#include <math.h>
#include <algorithm>
#include <set>
#include <deque>
#include <utility>

#define MAX 100005
using namespace std;


int main(void)
{
	int table[MAX];

	int i, j;
	int n;
	int temp1, temp2;
	int max = 0;
	int count = 0;
	bool ha[MAX];

	cin >> n;

	vector <int> a, b;


	for (i = 0; i < n; i++) {
		cin >> temp1 >> temp2;
		a.push_back(temp1);
		b.push_back(temp2);
		
	}

	for (i = 0; i < MAX; i++) {
		table[i] = 0;
	}

	sort(a.begin(), a.end());

	for (i = 0; i < n; i++) {
		if (a[i] > n) {
			a[i] = MAX + 2; //????????????????????????????????????
		}
		else {
			count++;
		}

		if (count + 1 < a[0]) {
			cout << max << endl;
			return (0);
		}

	}





	for (i = 0; i < n; i++) {
		table[a[i]]++;
		table[b[i]]--;

	}

	for (i = 1; i < MAX; i++) {
		table[i] = table[i] + table[i - 1];

	}

	for (i = 0; i < MAX; i++) {
		if (max < table[i]) {
			max = table[i];
		}
	}

	cout << max << endl;


	return (0);
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
  const int MAX = 100001;
  int n, a, b;
  int dp[MAX+1] = {};
   
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=2; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=2; i<MAX; i++) {
    if(dp[i]+1>=i) mx = i-1;
  }
  cout << mx << endl;
   
  return 0;
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	int i;
	int N=read();
	int ans=0;
	int a[100010]={0},b[100010]={0};
	int sum[N+3]={0};
	
	sum[0] = 1; //include me
	
	REP(i,N){
		a[read()] ++;
		b[read()+1] --;
	}
	
	for(i=1;i<=N+1;i++){
		sum[i] += sum[i-1] + a[i] + b[i];
		//cout << sum[i] << endl;
	}
	
	for(i=1;i<=N;i++){
		if(sum[i]>=i){
			ans = i-1;
		}
	}
	cout << ans << endl;
}

C++: #include <iostream>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;

int imos[100010];

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

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=1<<29,MOD=1e9+7;

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

C++: //#include "bits/stdc++.h"

#include <iostream>
#include <array>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

array<int, 100002> box; // use key 100001

int n;
int np;
int min_;
int max_;

int solve() {
	cin >> n;
	np = n + 1;
	for (int i = 0; i < n; i++) {
		cin >> min_ >> max_;
		if (min_ > np) {
			continue;
		}
		box[min_ - 1]--;
		max_ = min(max_, np);
		box[max_]++;
	}
	box[np + 2] = 0;
	for (int i = np + 1; i >= 0; i--) {
		box[i] += box[i+1];
		//std::cout << "debug " << i << " " << box[i] << std::endl; // debug
		if (i - 1 <= box[i]) {
			return i - 1;
		}
	}
	return 0;
}

int main() {

	cout << solve() << endl;

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

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

#include <vector>
#include <iostream>

using namespace std;

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

	int* hojo = new int[numOfFriends]; // ?????????????????????????????? 
	for (int i = 0; i < numOfFriends; i++) {
		hojo[i] = 0;
	}
	
	// hojo[i] ??????(i+1)?????§???????????¨???????????????????????????????????°????????\??????
	for (int i = 0; i < numOfFriends; i++) {
		int min, max;
		cin >> min;
		min--; // ????????????????????????
		cin >> max;
		max--; // ????????????????????????
		for (int j = min - 1; j <= max - 1 && j < numOfFriends ; j++) {
			hojo[j]++;
		}
	}

	int i = numOfFriends;
	for (; i >= 1; i--) {
		if (hojo[i - 1] >= i) {
			break;
		}
	}

	cout << i << endl;

	delete[] hojo;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(void){
    int n;
    cin>>n;
    int sum[100010]={};
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        sum[a]++;
        sum[b+1]--;
    }
    for(int i=0;i<100003;i++){
        sum[i+1]+=sum[i];
    }
    int ans=0;
    for(int i=0;i<=n;i++){
        if(i==sum[i])ans=i;
    }
    cout<<ans<<endl;
    return 0;
}