#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;

int op(int lhs, int rhs){return lhs + rhs;}
int e(){return 0;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<pair<int,int>> a(N);
    vector<int> ca(2 * N);
    for(int i = 0; i < N; i++){
        cin >> a[i].first >> a[i].second;
        ca[2 * i] = a[i].first;
        ca[2 * i + 1] = a[i].second;
    }
    sort(a.begin(), a.end());
    sort(ca.begin(), ca.end());
    ca.erase(unique(ca.begin(), ca.end()), ca.end());
    atcoder::segtree<int, op, e> seg(ca.size());
    for(auto [r, R] : a){
        r = lower_bound(ca.begin(), ca.end(), r) - ca.begin();
        R = lower_bound(ca.begin(), ca.end(), R) - ca.begin();
        int l = seg.min_left(r + 1, [&](int v){return v == 0;}) - 1;
        if(l != -1) seg.set(l, seg.get(l) - 1);
        seg.set(R, seg.get(R) + 1);
    }
    cout << seg.all_prod() - 1 << '\n';
}