Line Segments
Difficulty: Hard

You are given $N$ line segments. All of them lie on the same line. We will describe the $i^{th}$ line segment with two integers, $A_i$ and $B_i$. This means it starts at $A_i$ and ends at $B_i$. It will be denoted as $(A_i;B_i)$.  

Let's say a pair of line segments is ***good***, if one of them is not covering the other one. For example, the pair of $(1; 4)$ and $(2; 5)$ is ***good***. But the pair of $(1; 6)$ and $(2; 5)$ is ***bad*** because $(1; 6)$ covers $(2; 5)$.

Let's assume that we have the $i^{th}$ and $j^{th}$ line segments. If $A_i \le A_j$ and $B_i \ge B_j$, we can say "the $i^{th}$ line segment covers $j^{th}$ line segment."  

You must find the size of the biggest subset of these line segments that contains __no__ ***bad*** pairs.  

**Input Format**  
In the first line you are given an integer $N$, the total number of line segments.  
The next $N$ lines contain two integers, $A_i ~and ~B_i$.   

**Constraints**  
$1 \le N \le 10^5$  
$1 \le $ $A_i \le B_i \le 10^9$  

**Output Format**  
You must print one integer denoting the size of the biggest subset.  

**Sample Input#0**  

	4
    2 5
    1 4
    1 6
    3 8

**Sample Output#0**  

    3
    
**Explanation#0**  
We can only choose the first, second, and fourth line segments. We can not choose the third line segment because it covers the first and second line segments.  

**Sample Input#1**  

    5
    5 8
    3 7
    4 10
    12 15
    1 7

**Sample Output#1**  

    3


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (JavaScript):
    program lineSegments;

    type	tSegment =	record
                            l, r: longInt;
                        end;

    var	a: array[ 1..100000 ] of tSegment;

    function less( const a, b: tSegment ): boolean;
    begin
        if ( a.l <> b.l ) then
            exit( a.l < b.l );

        exit( a.r > b.r );
    end;

    procedure qsort( l, r: longInt );
    var	i, j: longInt;
        t, med: tSegment;
    begin
        if ( l < r ) then
        begin
            i := l;
            j := r;
            med := a[ l + random(r - l + 1) ];
            while ( i <= j ) do
            begin
                while ( less( a[i], med ) ) do
                    inc( i );

                while ( less( med, a[j] ) ) do
                    dec( j );

                if ( i <= j ) then
                begin
                    t := a[i];
                    a[i] := a[j];
                    a[j] := t;

                    inc( i );
                    dec( j );
                end;
            end;

            qsort( l, j );
            qsort( i, r );
        end;
    end;

    var	n, i, x, l, r, c, ans: longInt;
        f: array[ 0..100000 ] of longInt;

    begin
        readln( n );
        for i := 1 to n do
            readln( a[i].l, a[i].r );

        qsort( 1, n );

        ans := 0;
        f[0] := 0;
        for i := 1 to n do
        begin
            x := a[i].r;

            if ( x > f[ans] ) then
            begin
                inc( ans );
                f[ans] := x;

                continue;
            end;

            l := 1;
            r := ans;
            while ( l < r ) do
            begin
                c := ( l + r ) div 2;
                if ( f[c] >= x ) then
                    r := c
                else
                    l := c + 1;
            end;

            f[l] := x;
        end;

        writeln( ans );
    end.