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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
//using System.Text;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
//using System.Numerics;
//using static System.Math;
//using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    void chmax<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? b : a; }
    void chmin<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? a : b; }

    void solve()
    {
        int N = cin.nextint;
        int M = cin.nextint;

        var G = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            G[i] = new List<int>();
        }
        var U = new DisjointSet(N);
        for (int i = 0; i < M; i++)
        {
            int a = cin.nextint - 1;
            int b = cin.nextint - 1;
            G[a].Add(b);
            G[b].Add(a);
        }

        int cnt = 0;
        for (int i = 1; i < N; i++)
        {
            bool flag = true;
            foreach (var v in G[i])
            {
                if (U.Same(i - 1, v))
                {
                    flag = false;
                }
            }
            if (flag)
            {
                U.Unite(i, i - 1);
                cnt++;
            }
        }
        WriteLine(N - 1 - cnt);
    }

    class DisjointSet
    {
        int[] par;
        byte[] rank;
        /// <summary>
        /// 0 から <paramref name="N"/>-1 までの番号がついた <paramref name="N"/> 個の要素からなる集合を作成します．
        /// </summary>
        /// <param name="N">要素数</param>
        /// <remarks>このコンストラクタは O(N) で実行されます．</remarks>
        public DisjointSet(int N)
        {
            par = new int[N];
            rank = new byte[N];
            for (int i = 0; i < N; i++)
                par[i] = -1;
        }
        /// <summary>
        /// 指定した要素が属する集合の代表値を取得します．
        /// </summary>
        /// <param name="id">調べたい要素の 0-indexed での番号</param>
        /// <returns>指定した要素が属する集合の代表値</returns>
        /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
        public int this[int id]
        {
            get
            {
                if ((par[id] < 0)) return id;
                return par[id] = this[par[id]];
            }
        }
        /// <summary>
        /// 指定した 2 つの要素が属する集合同士を 1 つに統合することを試みます．
        /// </summary>
        /// <param name="x">最初の要素の 0-indexed での番号</param>
        /// <param name="y">2 つ目の要素の 0-indexed での番号</param>
        /// <returns>統合に成功したならば true，そうでなければ false．</returns>
        /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
        public bool Unite(int x, int y)
        {
            x = this[x]; y = this[y];
            if (x == y) return false;
            if (rank[x] < rank[y]) { var tmp = x; x = y; y = tmp; }
            par[x] += par[y];
            par[y] = x;
            if (rank[x] == rank[y])
                rank[x]++;
            return true;
        }
        /// <summary>
        /// 指定した要素が属する集合のサイズを取得します．
        /// </summary>
        /// <param name="x">指定する要素の 0-indexed での番号</param>
        /// <returns>集合のサイズ</returns>
        /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
        public int Size(int x) { return -par[this[x]]; }

        /// <summary>
        /// 指定した 2 つの要素が属する集合が同じかどうかを判定する．計算量 O(α(N))．
        /// </summary>
        /// <param name="x">最初の要素の 0-indexed での番号</param>
        /// <param name="y">2 つ目の要素の 0-indexed での番号</param>
        /// <returns>同じならば true，そうでなければ false．</returns>
        /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
        public bool Same(int x, int y) { return this[x] == this[y]; }

    }   
}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
}