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# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using static Atcoder;
using System.Threading.Tasks;
using System.ComponentModel;

public class Hello
{

    public static void Main()
    {
        int[] box = SpIntRead(' ');
        int N = box[0];
        int M = box[1];
        int[,] a = new int[100000, 2];
        int[] b = new int[2];
        for (int i = 0; i < M; i++)
        {
            b = SpIntRead(' ');
            a[i, 0] = b[0] - 1;
            a[i, 1] = b[1] - 1;
        }
        int count = 0;
        pos B = new pos();
        pos[] ans = new pos[100000];
        B.x = a[0, 0];
        B.y = a[0, 1];
        ans[0] = B;
        for (int i = 1; i < M; i++)
        {
            int c = 0;
            bool flag = true;
            foreach (pos P in ans)
            {
                //始点が内包される場合
                if (P.x <= a[i, 0] && a[i, 0] <= P.y)
                {
                    //終点も
                    if (P.x <= a[i, 1] && a[i, 1] <= P.y)
                    {
                        ans[c].x = a[i, 0];
                        ans[c].y = a[i, 1];
                        flag = false;
                        break;
                    }
                    else {
                        ans[c].x = a[i, 0];
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    count++;
                    ans[count].x = a[i, 0];
                    ans[count].y = a[i, 1];
                }
                c++;
            }
        }
        Console.WriteLine(count + 1);
    }
    struct pos
    {
        public int x;
        public int y;
    }
}


public class Atcoder
{
    static void Swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
    public static long LCM(long a, long b)
    {
        long c = a * b;
        if (a < b) Swap(ref a, ref b);
        long d = a % b;
        while (d != 0)
        {
            a = b;
            b = d;
            d = a % b;
        }
        return c / b;
    }
    public static string strReverce(string s)
    {
        return new string(s.Reverse().ToArray());
    }
    public static int intRead()
    {
        return int.Parse(Console.ReadLine());
    }
    public static bool IsPrime(int n)
    {
        if (n < 2) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;

        //double sqrtNum = Math.Sqrt(n);
        for (int i = 3; i < n; i += 2)
        {
            if (n % i == 0) return false;
        }
        return true;
    }
    public static string[] SpRead(char c)
    {
        return Console.ReadLine().Split(c);
    }
    public static int[] SpIntRead(char c)
    {
        return Console.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
    }

}