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: Time Limit Exceeded)

import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args){
        FastScanner fsc=new FastScanner();
        int n=fsc.nextInt();
        int m=fsc.nextInt();
        int[][] ab=new int[m][2];
        A[] a=new A[n-1];
        for(int k=0;k<n-1;k++){
            a[k]=new A();
        }
        for(int i=0;i<m;i++){
            ab[i][0]=fsc.nextInt()-1;
            ab[i][1]=fsc.nextInt()-1;
            for(int j=ab[i][0];j<ab[i][1];j++){
                a[j].reqs++;
                a[j].reqno.add(i);
            }
        }
        fsc.close();
        //Arrays.sort(ab, new C());
        int res=0;
        int manyreq=-1;
        int reqno=0;
        // for(int k=0;k<n-1;k++){
        //     System.out.println(req[k]);
        // }
        while(true){
            for(int k=0;k<n-1;k++){
                if(a[k].reqs>reqno){
                    reqno=a[k].reqs;
                    manyreq=k;
                }
            }
            if(manyreq==-1) break;
            for(int index:a[manyreq].reqno){
                for(int l=ab[index][0];l<ab[index][1];l++) a[l].reqs--;
                ab[index][0]=-1;
                ab[index][1]=-1;
            }
            res++;
            manyreq=-1;
            reqno=0;
            // for(int k=0;k<n-1;k++){
            //     System.out.println(req[k]);
            // }
        }
        System.out.println(res);
    }
    static class A{
        int reqs;
        HashSet<Integer> reqno;
        public A(){
            this.reqs=0;
            this.reqno=new HashSet<Integer>();
        }
    }
    static class C implements Comparator<int[]>{
        @Override
        public int compare(int[] x, int[] y){
            return x[0]>y[0]?1:x[0]<y[0]?-1:0;
        }
    }
    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte(){
            if(ptr<buflen) return true;
            else{
                ptr = 0;
                try{
                    buflen = in.read(buffer);
                }
                catch(IOException e){
                    e.printStackTrace();
                }
                if(buflen<=0) return false;
            }
        return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            if (!hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) throw new NumberFormatException();
            while(true){
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                }
                else if(b == -1 || !isPrintableChar(b)) return minus ? -n : n;
                else throw new NumberFormatException();
                b = readByte();
            }
        }
        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }
        public double nextDouble() { return Double.parseDouble(next());}
        public void close(){
            try{in.close();}
            catch(IOException e){e.printStackTrace();}
        }
    }
}