Problem:
Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N-1 bridges.

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

Request i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.

You decided to remove some bridges to meet all these M requests.

Find the minimum number of bridges that must be removed.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq a_i < b_i \leq N

All pairs (a_i, b_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the minimum number of bridges that must be removed.

Sample Input 1

5 2
1 4
2 5

Sample Output 1

1

The requests can be met by removing the bridge connecting the second and third islands from the west.

Sample Input 2

9 5
1 8
2 7
3 5
4 6
7 9

Sample Output 2

2

Sample Input 3

5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

Sample Output 3

4

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

//include
//-------------------------
#include<vector>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<string>
#include<cstring>
#include<ctime>

using namespace std;

//conversion
//-----------------------
inline int toInt(string s) { int v;istringstream sin(s);sin >> v;return v; }
template<class T>inline string toString(T x) { ostringstream sout;sout << x;return sout.str(); }

//math
//-----------------------
template<class T>inline T sqr(T x) { return x * x; }

//using
//-----------------------
using VI = vector<int>;
using VVI = vector<VI>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using TIII = tuple<int, int, int>;
using VTIII = vector<TIII>;
using LL = long long;

//function
enum priority_code {
	fs,
	sf,
};
template<class T = void, priority_code t = fs>
struct greater_pair {
	constexpr bool operator()(const T& _Left, const T& _Right)const {
		if (t == sf)
			return (_Left.second > _Right.second ||
			(!(_Right.second > _Left.second) && _Left.first > _Right.first));
		else
			return (_Left.first > _Right.first ||
			(!(_Right.first > _Left.first) && _Left.second > _Right.second));
	}
};
template<class T = void, priority_code t = fs>
struct less_pair {
	constexpr bool operator()(const T& _Left, const T& _Right)const {
		if (t == sf)
			return (_Left.second < _Right.second ||
			(!(_Right.second < _Left.second) && _Left.first < _Right.first));
		else
			return (_Left.first < _Right.first ||
			(!(_Right.first < _Left.first) && _Left.second < _Right.second));
	}
};


//container util
//-----------------------
#define WHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define RWHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(rbegin(whole), rend(whole), ##__VA_ARGS__); })(x)
#define CWHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(cbegin(whole), cend(whole), ##__VA_ARGS__); })(x)
#define CRWHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(crbegin(whole, crend(whole), ##__VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(auto&& i :c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define sort(c) sort((c).begin(),(c).end())
#define RESET(c) int _c=(c).size();(c).clear();(c).resize(_c);
#define EACH_INPUT(x) EACH(itr,x)cin>>*itr;
#define EACH_OUTPUT(x) EACH(itr,x)cout<<*itr<<endl;

// repetition
//----------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define repeat(i,n) FOR(i,0,n)

//constant
//----------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a).0,sizeof(a))

//debug
//#define DEBUG
#ifdef DEBUG
#define dump(x) cerr<<#x<<"="<<(x)<<endl;
#define dump_pair(x) cerr<<#x<<"=["<<(x).first<<", "<<(x).second<<" ]"<<endl;
#define dump_tuple3(x) cerr<<#x<<"=["<<get<0>((x))<<", "<<get<1>((x))<<", "<<get<2>((x))<<" ]"<<endl;
#define debug(x) cerr<<#x<<"="<<(x)<<"(L"<<__LINE__<<")"<<""<<__FILE__<<endl;
#else
#define dump(x)
#define dump_pair(x)
#define dump_tuple(x)
#define debug(x)
#endif
//#define STAY
#ifdef STAY
#define stay cout<<"Push any key and Enter..."; scanf_s("%*s");
#else
#define stay
#endif

int main() {

	int n, m;cin >> n >> m;
	vector<tuple<int, int, int>> border;//position, is begin, id
	repeat(i, m) {
		int f, l;cin >> f >> l;
		border.PB(make_tuple(f, 1, i));
		border.PB(make_tuple(l, 0, i));
	}
	sort(border);

	queue<int> idque;
	bool id[200000] = {};
	int res = 0;
	repeat(i, (border.size())) {
		if (get<1>(border[i])) {
			id[get<2>(border[i])] = true;
			idque.push(get<2>(border[i]));
		}
		else if(id[get<2>(border[i])]){
			while (!idque.empty()) {id[idque.front()] = false;	idque.pop();}
			++res;
		}

	}
	cout << res << endl;

	stay;
	return 0;
}