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++: using namespace std;
#include <vector>
#include <utility>
#include <functional>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

template < typename T >
using fun = function<T>;

template <typename T> class Stream {
  private:
    vector<T> value;

    template <typename F, typename U, typename B, typename E>
    auto fold(B iter, E end, F f, U u) -> decltype(f(u, *iter)) {
      if (iter != end) {
        u = f(u, *iter);
        iter++;
        for (; iter != end; iter++)
          u = f(u, *iter);
      }
      return u;
    }

    template <typename F, typename U, typename B, typename E>
    auto scan(B iter, E end, F f, U u) -> Stream<decltype(f(u, *iter))> {
      vector<T> v;
      v.push_back(u);
      if (iter != end) {
        u = f(u, *iter);
        v.push_back(u);
        iter++;
        for (; iter != end; iter++) {
          u = f(u, *iter);
          v.push_back(u);
        }
      }
      Stream<decltype(f(u, *iter))> s(v);
      return s;
    }

  public:
    Stream(initializer_list<T> &&v) {value = v;};
    Stream(vector<T> v) : value(v) {};
    Stream(int n) {value.resize(n);};
    Stream() {};
    
    Stream& operator= (initializer_list<T> &&v) {value = v;};
    T& operator[] (int n) {return value[n];};

    void push_back(T t) {
        value.push_back(t);
    }   

    size_t size() {
        return value.size();
    }   

    void reserve(size_t s) {
        value.reserve(s);
    }   

    void resize(size_t s) {
        value.resize(s);
    }

    vector<T> unwrap() {
      return value;
    }

    Stream<T> reverse() {
      vector<T> v = value;
      std::reverse(v.begin(), v.end());
      return Stream<T>(v);
    }

    template <typename U>
    Stream<pair<T, U>> zip(Stream<U> s) {
      Stream<pair<T, U>> z;
      z.reserve(min(value.size(), s.size()));
      for (size_t i=0; i<min(value.size(), s.size()); i++)
        z.push_back(make_pair(value[i], s[i]));
      return z;
    }

    template <typename F>
    auto map(F f) -> Stream<decltype(f(value[0]))> {
      vector<decltype(f(value[0]))> c;
      c.reserve(value.size());
      for (auto x: value) c.push_back(f(x));
      return Stream<decltype(f(value[0]))>(move(c));
    }

    template <typename F>
    auto foreach(F f) -> decltype(f(value[0])) {
      for (auto x: value) f(x);
    }

    Stream<T> filter(fun<bool(T)> f) {
      vector<T> c;
      for (auto x: value) if (f(x))
        c.push_back(x);
      return Stream<T>(move(c));
    }

    template <typename F, typename U>
    auto foldr(F f, U u) -> decltype(f(u, value[0])) {
      return fold(value.rbegin(), value.rend(), f, u);
    }

    template <typename F, typename U>
    auto foldl(F f, U u) -> decltype(f(u, value[0])) {
      return fold(value.begin(), value.end(), f, u);
    }

    template <typename F, typename U>
    auto scanr(F f, U u) -> Stream<decltype(f(u, value[0]))> {
      return scan(value.rbegin(), value.rend(), f, u);
    }

    template <typename F, typename U>
    auto scanl(F f, U u) -> Stream<decltype(f(u, value[0]))> {
      return scan(value.begin(), value.end(), f, u);
    }

    Stream<T> take(size_t n) {
      vector<T> v;
      for (size_t i=0; i<value.size() && i<n; i++)
        v.push_back(value[i]);
      return Stream<T>(v);
    }

    Stream<T> drop(size_t n) {
      vector<T> v;
      for (size_t i=n; i<value.size(); i++)
        v.push_back(value[i]);
      return Stream<T>(v);
    }

    void debug() {
      for (auto x: value) 
        cout << x << " ";
      cout << endl;
    }

    auto begin() -> decltype(value.begin()) { 
      return value.begin(); 
    }

    auto end() -> decltype(value.end()) { 
      return value.end(); 
    }
};

int main(){
  int n;
  cin >> n;
  vector<int> imos(100001+10, 0);
  for (int i=0; i<n; i++) {
    int n, m; cin >> n >> m;
    imos[n]++; imos[m+1]--;
  }
  vector<int> z(imos.size());
  for (int i=0; i<z.size(); i++) z[i] = i-1;

  auto xs = Stream<int>(imos)
    .scanl([](int x, int y){return x + y;}, 0) 
    .drop(1)
    .zip(Stream<int>(z))
    .map([](auto p){return p.first>=p.second ? min(p.first, p.second) : 0;})
    .unwrap();
  cout << *max_element(xs.begin(), xs.end()) << endl;
  return 0;
}

C++: #include<iostream>

using namespace std;

int r[100100];
int main(){

    int N;//<=10^5
    cin>> N;
    int a[N], b[N];
    for(int i=0; i<N; i++) cin>> a[i]>> b[i];

    for(int i=0; i<N; i++){
        r[a[i]]++;
        r[b[i]+1]--;
    }
    for(int i=0; i+1<100100; i++){
        r[i+1]+=r[i];
    }
    int s=1;
    for(int i=0; i<100100; i++){
        if(r[i]>=i) s=i;
    }

    cout<< s-1<< endl;

    return 0;
}

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

int s[100010], n;

int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0);

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

    int res=0;
    for(int i=1; i<=n; ++i) {
        s[i]+=s[i-1];
        if (s[i]==i) res=i-1;
    }
    cout<<res<<endl;
}

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);
  rep(i,n){
    cin >> a >> b;
    if(a) a--;
    if(b) b--;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,n){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  rep(i,n+1){
    k = max(k,ans[i]); 
  }
  if(k == 1) cout << 0 << endl;
  else
    cout << k << endl;
  return 0;
}