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: Wrong Answer)

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int m = scn.nextInt();
		int bmin = Integer.MAX_VALUE;
		int ans = 0;
		long[] data_set = new long[m];
		for (int i = 0; i < m; i++) {
			int a = scn.nextInt();
			int b = scn.nextInt();
			data_set[i] = a * 1000000 + b;
		}
		
		data_set = sort(data_set);
		
		
		for (int i = 0; i < m; i++) {
			long buf = data_set[i];
			long a = buf / 1000000;
			long b = buf % 1000000;

			if (bmin <= a) {
				ans++;
				bmin = Integer.MAX_VALUE;
			}
			bmin = Math.min(bmin, (int) b);
		}
		System.out.println(ans + 1);
		scn.close();
	}

	public static long[] sort(long[] data_set) {
		long[] b = new long[data_set.length];
		int middle = (b.length - 1) / 2;
		long[] a1 = new long[middle + 1];
		long[] a2;

		if (b.length > 0) {
			a2 = new long[b.length - middle-1];
		} else {
			a2 = new long[0];
		}

		if (b.length < 2) {
			return data_set;
		}

		System.arraycopy(data_set, 0, a1, 0, middle + 1);
		System.arraycopy(data_set, middle + 1, a2, 0, b.length - middle - 1);
		a1 = sort(a1);
		a2 = sort(a2);

		int bi = 0;
		int a1i = 0;
		int a2i = 0;

		for (int i = 0; i < b.length - 1; i++) {
			if (a1i < a1.length && a2i < a2.length) {
				if (a1[a1i] < a2[a2i]) {
					b[bi] = a1[a1i];
					a1i++;
				} else {
					b[bi] = a2[a2i];
					a2i++;
				}
				bi++;
			}
		}
		if (a1i > a2i) {
			for (int j = a2i; j < a2.length; j++, bi++) {
				b[bi] = a2[j];
			}
		} else {
			for (int j = a1i; j < a1.length; j++, bi++) {
				b[bi] = a1[j];
			}
		}
		return b;
	}
}