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

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

#define int long long

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

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

const int N = 100010;
int in[N];
int de[N];

int main(){
  int n;
  while(cin>>n){
    rep(i,N)in[i] = 0,de[i] = 0;
    rep(i,n){
      int a,b;
      cin>>a>>b;
      in[a]++;
      de[b+1]++;
    }
    int ans = 0;
    int now = 0;
    rep(i,N){
      now += in[i];
      now -= de[i];
      if (now >= i)ans = i;
    }
    if (ans != 0)ans--;
    cout << ans << endl;
  }
}

C++: #include<iostream>
int main()
{
	int imos[100001];
	for(int i = 0; i < 100001; i++)
	{
		imos[i] = 0;
	}
	int N;
	std::cin >> N;
	int range_min = 0, range_max = 100000;
	for(int i = 0; i < N; i++)
	{
		int a, b;
		std::cin >> a >> b;
		imos[a - 2]++;
		imos[b - 1]--;
		if(range_min > a - 2)
		{
			range_min = a - 2;
		}
		if(range_max < b - 1)
		{
			range_max = b - 1;
		}
	}
	for(int i = range_min; i < range_max; i++)
	{
		imos[i + 1] += imos[i];
	}
	int ninzu_max = 1;
	for(int i = range_min; i < range_max; i++)
	{
		if(i + 2 <= imos[i])
		{
			ninzu_max = i + 2;
		}
	}
	std::cout << ninzu_max - 1 << std::endl;
	return 0;
}

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 < 100001; i++) {
    sum += invite[i];
    if(i <= sum)
      res = i;
  }
  cout << res << endl;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cassert>
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define DEC(i, k) for (int i = (k); i >= 0; --i)
#define FORIT(i, c) for (__tyoeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SZ(v) (int)((v).size())
#define MEMSET(v, h) memset((v), (h), sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())
#define ALL(c) (c).begin(), (c).end()

struct FenwickTree {
    int tree[100100];
    void change(int index, int value) {
        for (index++; index < 100010; index += index & -index) {
            tree[index] += value;
        }
    }
    int sum(int index) {
        int ret = 0;
        for (index++; index > 0; index -= index & -index) {
            ret += tree[index];
        }
        return ret;
    }
};

FenwickTree ftree;

int main()
{
    memset(&ftree, 0, sizeof(ftree));
    ftree.change(0, 1);
    int n;
    scanf("%d", &n);
    REP(i, n) {
        int a, b;
        scanf("%d %d", &a, &b);
        ftree.change(a, 1);
        ftree.change(b + 1, -1);
    }
    int ans = 0;
    FOREQ(i, 1, n + 1) {
        if (ftree.sum(i) >= i) {
            ans = i - 1;
        }
    }
    printf("%d\n", ans);
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define int long long

const int MAX = 100001;

int n;
int a[MAX], b[MAX];

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  int imos[MAX + 2];
  fill(imos, imos + MAX + 2, 0);
  imos[0]++;
  for (int i = 0; i < n; i++) {
    imos[a[i]]++;
    imos[b[i] + 1]--;
  }

  /*
  for (int i = 1; i <= 10; i++) {
    cout << imos[i] << " ";
  }
  cout << endl;
  */
  for (int i = 1; i < MAX + 2; i++) {
    imos[i] += imos[i - 1];
  }
  for (int i = n; i >= 2; i--) {
    if (imos[i] >= i) {
      cout << i - 1 << endl;
      /*
  for (int i = 1; i <= 10; i++) {
    cout << imos[i] << " ";
  }
  cout << endl;
  */
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}