#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
using PII = pair<ll, ll>;
#define FOR(i,a,n) for(ll i=(ll)a; i<(ll)n; ++i)
#define REP(i,n) FOR(i,0,n)
const ll INF = 1LL << 60;
template<typename T> void chmin(T &a, T b) { a = min(a, b); }

int main() {
	int N;
	cin >> N;
	vector<pair<int, int>>v(N);
	vector<pair<int, int>>w(N);
	for (auto &i : v)cin >> i.first >> i.second;
	for (int i = 0; i < N; i++) {
		w[i] = { v[i].second,v[i].first };
	}
	sort(v.begin(), v.end());
	sort(w.begin(), w.end());
	int vindex = 0, windex = 0;
	vector<int>vused(N + 2);
	vector<int>wused(N + 2);
	set<pair<int, pair<int, int>>>vyoyaku;
	set<pair<int, pair<int, int>>>wyoyaku;
	set<pair<int, int>>vkouho;
	set<pair<int, int>>wkouho;
	vkouho.insert({ 0,1 });
	int ans = 0;
	while (vindex < N || windex < N) {
		//cout << vindex << " " << windex << endl;
		int nx = 0;
		if (vindex == N)nx = 1;
		else if (windex<N&&v[vindex].first>w[windex].first)nx = 1;
		//cout << vindex << " " << windex << " " << nx << endl;
		if (nx == 0) {// use v
					  //	cout << "vvvvvv" << endl;
			while (!vyoyaku.empty() && vyoyaku.begin()->first <= v[vindex].first) {
				auto box = vyoyaku.begin()->second;
				vyoyaku.erase(vyoyaku.begin());

				if (vused[box.second]) {
					if (vused[box.second]>box.first) {
						vkouho.insert(box);
						vkouho.erase({ vused[box.second],box.second });
						vused[box.second] = box.first;
					}
					auto it = vkouho.find(box);
					while (it != vkouho.begin() && prev(it)->first >= box.first) {
						vused[prev(it)->second] = 0;
						vkouho.erase(prev(it));
					}
				}
				else {
					auto nx = vkouho.lower_bound(box);
					if (nx != vkouho.begin() && prev(nx)->second >= box.second)continue;
					vkouho.insert(box);
					auto it = vkouho.find(box);
					while (next(it) != vkouho.end() && next(it)->second <= box.second) {
						vused[next(it)->second] = 0;
						vkouho.erase(next(it));
					}
				}

			}

			auto it = vkouho.lower_bound({ v[vindex].second,MOD });
			if (it == vkouho.begin()) {
				vindex++;
				continue;
			}
			it = prev(it);
			int score = it->second;
			//	cout << v[vindex].first << " " << v[vindex].second << " " << score << endl;
			ans = max(ans, score);
			wyoyaku.insert({ v[vindex].first+1,{ v[vindex].second + 1,score + 1 } });
			vindex++;
		}
		else {//use w
			  //cout << w[windex].first << " " << wyoyaku.size() << endl;
			while (!wyoyaku.empty() && wyoyaku.begin()->first <= w[windex].first) {
				auto box = wyoyaku.begin()->second;
				wyoyaku.erase(wyoyaku.begin());

				if (wused[box.second]) {
					if (wused[box.second]>box.first) {
						wkouho.insert(box);
						wkouho.erase({ wused[box.second],box.second });
						wused[box.second] = box.first;
					}
					auto it = wkouho.find(box);
					while (it != wkouho.begin() && prev(it)->first >= box.first) {
						wused[prev(it)->second] = 0;
						wkouho.erase(prev(it));
					}
				}
				else {
					auto nx = wkouho.lower_bound(box);
					if (nx != wkouho.begin() && prev(nx)->second >= box.second)continue;
					wkouho.insert(box);
					auto it = wkouho.find(box);
					while (next(it) != wkouho.end() && next(it)->second <= box.second) {
						wused[next(it)->second] = 0;
						wkouho.erase(next(it));
					}
				}

			}
			//	cout << "hihihh" << endl;
			//	cout <<"wwwwww "<< w[windex].first << " " << w[windex].second << " " << wkouho.size() << endl;
			auto it = wkouho.lower_bound({ w[windex].second,MOD });
			if (it == wkouho.begin()) {
				windex++;
				continue;
			}
			it = prev(it);
			int score = it->second;
			//	cout << w[windex].first << " " << w[windex].second << " " << score << endl;
			ans = max(ans, score);
			vyoyaku.insert({ w[windex].first+1,{ w[windex].second + 1,score + 1 } });
			windex++;
		}


	}
	cout << ans << endl;
	//cin >> N;
}



Measured memory_bytes: 9876