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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.do_main();
    }
    public void do_main() {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int N = scan.nextInt();
        int M = scan.nextInt();
        Complain[] complains = new Complain[M];
        for (int i = 0; i < M; i++) {
            Complain b = new Complain();
            b.from = scan.nextInt()-1;
            b.to = scan.nextInt()-1;
            complains[i] = b;
        }
        int answer = solve(N, M, complains);
        System.out.println(answer);
    }

    public int solve(int N, int M, Complain[] complains) {
        Arrays.sort(complains, new FromComplainComparator());
        int[] group = new int[N];
        Arrays.fill(group, 0);
        int left_answer = 0;
        int left_index = -1;
        int right_index = -1;
        for (int i = 0; i < M; i++) {
            Complain c = complains[i];
            if (group[c.from] != group[c.to]) {
                continue;
            }
            right_index = c.to;
            int j = i + 1;
            for (; j < M; j++) {
                Complain a = complains[j];
                if (right_index <= a.from) {
                    break;
                }
                right_index = Math.min(right_index, a.to);
            }
            for (int k = right_index; k < N; k++) {
                if (group[k] != group[c.from]) {
                    break;
                }
                group[k] = right_index;
            }
            left_answer += 1;
        }
        Arrays.sort(complains, new ToComplainComparator());
        Arrays.fill(group, N-1);
        int right_answer = 0;
        for (int i = 0; i < M; i++) {
            Complain c = complains[i];
            if (group[c.from] != group[c.to]) {
                continue;
            }
            left_index = c.from;
            int j = i + 1;
            for (; j < M; j++) {
                Complain a = complains[j];
                if (a.to <= left_index) {
                    break;
                }
                left_index = Math.max(left_index, a.from);
            }
            for (int k = left_index; 0 <= k; k--) {
                if (group[k] != group[c.to]) {
                    break;
                }
                group[k] = left_index;
            }
            right_answer += 1;
        }
        return Math.min(left_answer, right_answer);
    }

    class Complain {
        int from = 0;
        int to = 0;
    }
    class FromComplainComparator implements Comparator<Complain> {
        @Override
        public int compare(Complain o1, Complain o2) {
            int result = Integer.compare(o1.from, o2.from);
            if (result != 0) {
                return result;
            }
            int distance_1 = o1.to - o1.from;
            int distance_2 = o2.to - o2.from;
            return Integer.compare(distance_1, distance_2);
        }
    }
    class ToComplainComparator implements Comparator<Complain> {
        @Override
        public int compare(Complain o1, Complain o2) {
            int result = Integer.compare(o1.to, o2.to);
            if (result != 0) {
                return -result;
            }
            int distance_1 = o1.to - o1.from;
            int distance_2 = o2.to - o2.from;
            return Integer.compare(distance_1, distance_2);
        }
    }
}