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 <ccomplex>
#include <vector>
#include <cassert>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <numeric>
#include <list>
#include <assert.h>
#include <math.h>
#include <valarray>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <complex>
#include <list>
#include <time.h>
#include <stack>
#include <locale>
#include <clocale>
#include <ctype.h>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(),(c).end())
#define BACKSORT(c) sort((c).begin(),(c).end(),std::greater<LL>())
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

int xcomp(vector<LL>& a, vector<LL>& b) {
	return a[0] < b[0];
}

#define XSORT(c) sort((c).begin(),(c).end(),xcomp)

#define ALL(x) (x).begin(),(x).end()
LL mod = 1000000007;

int main() {
	LL N;
	cin >> N;
	vector<LL> v(100001, 0);
	REP(i, N) {
		LL a, b;
		cin >> a >> b;
		a--; b--;
		for (LL j = a; j <= b; ++j) {
			v[j]++;
		}
	}

	for (LL j = N; j >= 0; --j) {
		if (v[j] >= j) {
			cout << j << endl;
			break;
		}
	}
}

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

int a[100000] = {};

int main(void)
{
	int max;
	cin >> max;
	a[0]++;
	for(int i=0;i<max;++i){
		int c,b;
		cin >> c >> b;
		a[c]++;a[b+1]--;
	}
	int sum=0;
	int maxn=1;
	for(int i=0;i<=max;++i){
		sum+=a[i];
		if(i-1 <= sum)
		{
			maxn = i;
		}
	}
	cout << maxn-1 << endl;
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  vector<int> d(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    if( a == b) d[a]++;
    else{
      c[a]++;
      c[b]--;
    }
  }

  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  for(int i = 0;i<c.size();i++){
    c[i] += d[i];
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i == c[i]+1) maxnum = i-1;
  }
  cout << maxnum << endl;
  return 0;
}

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

int table[100001][100001];

int main(){
  int N;
  int a, b;

  cin >> N;
  for(int i = 0; i < N; i++){
    cin >> a >> b;
    table[i][a-1] = 1;
    table[i][b] = -1;
  }
  for(int i = 0; i < N; i++){
    for(int j = 1; j < N; j++){
      table[i][j-1] += table[i][j];
    }
  }

  for(int i = 1; i < 100001; i++){
    for(int j = 0; j < N; j++){
      table[i][j] += table[i-1][j];
    }
  }
  int max = 0;
  for(int i = 0; i < 100001; i++){
    if(max < table[100000][i]) max = table[100000][i];
  }
  cout << max << endl;
}

C++: #include<iostream>
#include<string.h>

using namespace std;

int main(){
	int n;
	cin >> n;
	
	int l[100002],r[100002];
	memset(l,0,sizeof(l));
	memset(r,0,sizeof(r));
	
	for(int i = 0;i < n;i++){
		int a,b;
		cin >> a >> b;
		l[a]++;
		r[b]++;
	}
	
	int can = 0;
	int ans = 0;
	for(int i = 1;i <= n+1;i++){
		can += l[i+1];
		if(can >= i){
			ans = i;
		}
		can -= r[i+1];
	}
	
	cout << ans << 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[100000], b[100000];
int x[200000];

int main(){
	cin >> n;
	rep(i,n){
		cin >> a[i] >> b[i];
		a[i]--; b[i]--;
		x[a[i]]++;
		x[b[i]+1]--;
	}
	for(int i = 1; i < n+100; i++) x[i] += x[i-1];
	int ans = 0;
	for(int i = 1; i < n+1; i++){
		if(x[i] >= i) ans = i;
	}
	cout << ans << endl;
}

C++: #include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int i;
  int n;
  cin>>n;
  int a[100000],b[100000];
  for(i=0;i<n;++i)
    cin>>a[i]>>b[i];
  int mn=1,mx=n+2;
  while(mn+1!=mx){
    int ct=1;
    for(i=0;i<n;++i){
      if(a[i]<=(mn+mx)/2&&(mn+mx)/2<=b[i])
	++ct;
    }
    if(ct<(mn+mx)/2)
      mx=(mn+mx)/2;
    else
      mn=(mn+mx)/2;
  }
  cout<<mn-1<<endl;
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define S 100005
using namespace std;
int l[S]={0};

int main(){
    int N,a,b;
    cin>>N;
    l[1]=1;
    for(int i=0;i<N;i++){
        cin>>a>>b;
        l[a]++;
        l[b+1]--;
    }
    for(int i=2;i<S;i++){
        l[i]+=l[i-1];
    }
    int val=0;
    for(int i=1;i<S;i++){
        if(l[i]>=i){val=i;}
    }        
    cout<<val-1<<endl;
    return 0;
}

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

using namespace std;

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

const int nmax = 100005;
int imos[nmax];

int main() {
    std::ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n) {
        int a, b;
        cin >> a >> b;
        imos[a]++;
        imos[b+1]--;
    }
    for (int i = 1; i < nmax; i++)
        imos[i] += imos[i-1];
    int res;
    rep(i,nmax) {
        if (imos[i] >= i - 1)res = i - 1;
    }
    cout << res << 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(n+1);
  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;
}