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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::cmp::Ordering;

/// Equivalent to std::lowerbound and std::upperbound in c++
pub trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &T) -> usize;
}

impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }

    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}






use std::cmp;
use std::cmp::min;
use std::collections::BTreeMap;
use std::process;
use std::cmp::Ord;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::collections::BTreeSet;
#[derive(PartialEq, Eq)]
pub struct point(i64, i64, i64);
impl Ord for point {
    fn cmp(&self, other: &point) -> Ordering {
        self.0.cmp(&other.0)
    }
}

impl PartialOrd for point {
    fn partial_cmp(&self, other: &point) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
/*
impl PartialEq for point {
    fn eq(&self, other: &point) -> bool {
        self.0 == other.0
    }
}
impl Eq for point {
    fn eq(&self, other: &point) -> bool {
        self.0 == other.0
    }s
}*/
type P = (i64 , i64);
fn bfs(start:&P, goal:&P, map:&Vec<Vec<char>>) -> i64 {
        let dx = vec![1, 0, -1, 0];
        let dy = vec![0, 1, 0, -1];
        let s = (start.0, start.1);
        let g = (goal.0 , goal.1);
        let mut queue:VecDeque<P> = VecDeque::new();
        let mut d:Vec<Vec<i64>> = Vec::new();
        for i in 0..map.len(){
            d.push(Vec::new());
            for j in 0..map[i].len(){
                d[i].push(30000000);
            }
        }
        queue.push_back((s.0, s.1));
        d[s.0 as usize][s.1 as usize] = 0;
        while(queue.len()!=0){
            let mut p:P = queue[0];
            queue.pop_front();
            if p.0 == g.0 && p.1 == g.1{break;}
            for i in 0..4{
                let nx = p.0 + dx[i];
                let ny = p.1 + dy[i];
                if (0<=nx && nx <map.len() as i64 && 0<=ny &&ny<map[0].len() as i64 && map[nx as usize][ny as usize]!='#' && d[nx as usize][ny as usize] == 30000000 ){
                    queue.push_back((nx, ny));
                    d[nx as usize][ny as usize] = d[p.0 as usize][p.1 as usize] + 1;
                }
            }

        }
        d[goal.0 as usize][goal.1 as usize]
}

fn bfs01(start:&P, goal:&P, map:&Vec<Vec<char>>) -> i64 {
        let dx = vec![1, 0, -1, 0];
        let dy = vec![0, 1, 0, -1];
        let s = (start.0, start.1);
        let g = (goal.0 , goal.1);
        let mut queue:VecDeque<P> = VecDeque::new();
        let mut d:Vec<Vec<i64>> = Vec::new();
        for i in 0..map.len(){
            d.push(Vec::new());
            for j in 0..map[i].len(){
                d[i].push(30000000);
            }
        }
        queue.push_back((s.0, s.1));
        d[s.0 as usize][s.1 as usize] = 0;
        while(queue.len()!=0){
            let mut p:P = queue[0];
            queue.pop_front();
            if p.0 == g.0 && p.1 == g.1{break;}
            for i in 0..4{
                let nx = p.0 + dx[i];
                let ny = p.1 + dy[i];
                if (0<=nx && nx <map.len() as i64 && 0<=ny &&ny<map[0].len() as i64 && d[nx as usize][ny as usize] == 30000000 ){
                    if map[nx as usize][ny as usize] == '#'{
                        queue.push_back((nx, ny));
                        d[nx as usize][ny as usize] = d[p.0 as usize][p.1 as usize] + 1;
                    }
                    else{
                        queue.push_front((nx, ny));
                        d[nx as usize][ny as usize] = d[p.0 as usize][p.1 as usize];
                    }
                }
            }

        }
        d[goal.0 as usize][goal.1 as usize]
}
fn main() {
    input!{
        n:usize,
        m:usize,
        ss:[(i64, i64);m],
    };
    
    let mut s:Vec<(i64, i64)> = Vec::new();
    
    for i in 0..m{
        s.push((ss[i].1, ss[i].0));    
    }
    s.sort();
    let mut t = 0;
    let mut ans = 0;
    for i in 0..s.len(){
        if t <= s[i].1{
            t = s[i].0;
            ans+=1;
        }
    }
    println!("{}", ans);
}