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 <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-1<=sum[i])ans=i-1;
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include <iostream>
#define llint long long

using namespace std;

llint n;
llint dif[100005];

int main(void)
{
	cin >> n;
	llint l, r;
	for(int i = 1; i <= n; i++){
		cin >> l >> r;
		dif[l]++, dif[r+1]--;
	}
	for(int i = 1; i < 100005; i++) dif[i] += dif[i-1];
	
	llint ans = 0;
	for(int i = n; i >= 1; i--){
		if(dif[i+1] >= i){
			ans = i;
			break;
		}
	}
	cout << ans << endl;
	
	return 0;
}

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

#define rep(i,n) for (int i = 0; i < (n); ++i)

int n, a, b, friends[100005], ans, maxn, ansi;

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

    std::cin >> n;
    rep(i,n) {
        std::cin >> a >> b;
        if (a > n+1) {
            maxn++;
            continue;
        } else if (b > n+1-maxn) {
            b = n+1-maxn;
        }
        for (int i = a; i <= b; ++i) friends[i]++;
    }
    //for (int i = 0; i <= n+1; ++i) std::cout << friends[i] << std::endl;
    rep(i,n+1) {
        if (ansi < friends[i]) {
            ans = i-1;
        }
    }
    std::cout << ans << std::endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
??
using std::cin;
using std::cout;
using std::endl;
??
int main(void) {
????????int N;
????????cin >> N;
????????std::vector<int> frimin(N);
????????std::vector<int> frimax(N);
????????for (int i = 0; i < N; i++) {
????????????????cin >> frimin[i] >> frimax[i];
????????}
??
????????std::sort(frimin.begin(), frimin.end());
????????std::sort(frimax.begin(), frimax.end());
??
????????int ret = 0;
????????int count = 1;
????????int imin = -1;
????????int imax = -1;
????????for (int f = 2; f < N+2 && f <= frimax[N-1]; f++) {
????????????????for (int i = imin+1; frimin[i] == f; i++) {
????????????????????????count++;
????????????????????????imin++;
????????????????}
????????????????for (int i = imax+1; frimax[i] == f-1; i++) {
????????????????????????count--;
????????????????????????imax++;
????????????????}
??
????????????????if ( count >= f && count != 1 ) {
????????????????????????ret = f-1;
????????????????}
????????}
??
????????cout << ret << 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 a[100002]={0},b[100002]={0};
	int sum[N+1]={0};
	
	sum[0] = 1; //include me
	
	REP(i,N){
		a[read()] ++;
		b[read()] --;
	}
	
	for(i=1;i<=N;i++){
		sum[i] += sum[i-1] + a[i] + b[i];
		//cout << sum[i] << endl;
	}
	
	for(i=N;i>=0;i--){
		if(sum[i]>=i){
			cout << i-1 << endl;
		}
	}
	cout << 0 << endl;
}

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

int main() {

    int n, imos[100003] = {0};

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

    for(int i = 1; i < 100003; i++) {
        imos[i] += imos[i - 1];
    }

    int maxi = 0;
    for(int i = 0; i <= n + 1; i++) {
        if(imos[i] >= i) maxi = max(maxi, i);
    }
    cout << maxi - 1 << endl;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main(){
  int n;
  cin >> n;
  vector<int> fri(100003,0);
  int first,end,max_friend=0;
  REP(i,n){
    cin >> first >> end;
    fri[first]++;
    fri[end+1]--;
  }

  for(int i = 1;i<100003;i++){
    fri[i] +=fri[i-1];
    if(fri[i]  >= i-1)
      max_friend = max(i-1,max_friend);
  }
  cout << max_friend << endl;
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int (i)=0;(i)<(N);i++)

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

    const int MAX_RANGE = 100001 + 1;
    vector<int> table(MAX_RANGE, 0);
    table[0]++;
    table[MAX_RANGE]--;

    rep(i,N){
        int a,b;
        cin >> a >> b;
        table[a]++;
        table[b + 1]--;
    }

    int ans = 0;
    for(int i=1;i < MAX_RANGE; i++){
        table[i] += table[i-1];
        if(table[i] >= i) ans = i - 1;
    }

    cout << ans << endl;
}

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

int range[MAX][3];
int number[MAX];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> range[i][0] >> range[i][1];
    range[i][2] = 1;
  }
  
  for (int i = 2; i <= MAX; i++)
    number[i] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = range[i][0]; j <= range[i][1]; j++) {
      number[j]++;
    }
  }

  int ans = 0;
  for (int i = 2; i <= n + 1; i++) {
    if (number[i] >= i) ans = i - 1;
  }
  cout << ans << endl;
    
  return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,a,b;
  cin >> n;
  vector<ll> ans(1000010);
  ans[0] = 0;
  rep(i,n){
    cin >> a >> b;
    /* if(a) a--; */
    /* if(b) b--; */
    --a;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,100001){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  REP(i,1,n){
    k = max(k,ans[i]); 
  }
  if(k <= 1) cout << 0 << endl;
  else
    cout << k-1 << endl;
  return 0;
}