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 (Python):
n,m = map(int, input().split())
a = []
for i in range(m):
  x,y = map(int, input().split())
  a.append((x,y))
a.sort(key=lambda x:x[1])
ans = 1
l,r = a[0]
for x,y in a[1:]:
  if r <= x:
    ans += 1
    l = x
    r = y

print(ans)

Solution (Python):
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]
count = 0
current_bridge = float('inf')
AB.sort(key=lambda x:-x[0])

for a,b in AB:
    
    if b < current_bridge:
        count += 1
        current_bridge = a + 0.5
print(count)

Solution (Python):
N,M=map(int,input().split())

wars=[None]*M
for i in range(M):
  a,b=map(int,input().split())
  wars[i]=a-1,b-1

wars=sorted(wars,key=lambda x:x[1])

ans=0
cut=-1
for i in range(len(wars)):
  a,b=wars[i]
  if a>cut:
    cut=b-1
    ans+=1

print(ans)

Solution (Python):
import operator

n, m = map(int, input().split())
bridges = [0]
ms1 = (tuple(map(int, input().split())) for i in range(m))
ms2 = sorted(ms1, key=operator.itemgetter(1))

for i in range(m):
    if ms2[i][0] <= bridges[-1]:
        pass
    else:
        bridges.append(ms2[i][1] - 1)

if __name__ == "__main__":
    print(len(bridges)-1)

Solution (Python):
import operator

n, m = map(int, input().split())
bridges = [0]
ms1 = (tuple(map(int, input().split())) for i in range(m))
ms2 = sorted(ms1, key=operator.itemgetter(1))

for i in range(m):
    if ms2[i][0] <= bridges[-1]:
        pass
    else:
        bridges.append(ms2[i][1] - 1)

if __name__ == "__main__":
    print(len(bridges)-1)

Solution (Python):
n,m = map(int,input().split())
li = []
cnt = 0
for i in range(m):
    a,b = map(int,input().split())
    li.append((a,b))
li = sorted(li,key = lambda x:x[1])
prev = -1
for x in li:
    if(prev > x[0]):
        continue
    else:
        cnt += 1
    prev = x[1]
print(cnt)

Solution (Python):









N, M = map(int, input().split())
demand_list = []
for _ in range(M):
    ai, bi = map(int, input().split())
    demand_list.append([ai, bi])


demands = sorted(demand_list, key=lambda d: d[1])



cnt = 0
cut_max_len = -1

for demand in demands:
    a, b = demand
    
    if a < cut_max_len:
        continue
    
    

    
    
    cut_max_len = b
    cnt += 1


print(cnt)

Solution (Python):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]

sAB = sorted(AB, key=lambda x: x[1])

cnt = 0
for itr, ab in enumerate(sAB):
  a, b = ab
  if itr == 0:
    x = b -1
    cnt += 1
  else:
    if a > x:
      x = b -1
      cnt += 1
print(cnt)

Solution (Python):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

ab.sort(key=lambda x: x[1])

prev = -1
ans = 0
for a, b in ab:
    if a <= prev < b:
        continue
    else:
        ans += 1
        prev = b - 1

print(ans)

Solution (Python):
from operator import itemgetter

n, m = map(int, input().split())
ab = []
for _ in range(m):
    ab.append(list(map(int, input().split())))
abs = sorted(ab, key=itemgetter(1))
las = abs[0][1]
count = 1
for i in range(1, m):
    if las - 1 < abs[i][0]:
        las = abs[i][1]
        count += 1
print(count)

Solution (Python):
n,m = map(int,input().split())

abl = []
for _ in range(m):
    a,b = map(int,input().split())
    abl.append((a,b))
    
abl = sorted(abl)

ans = 0
tail = 0
for a,b in abl:
    if tail <= a:
        ans += 1
        tail = b
    else:
        if b < tail:
            tail = b
            
print(ans)

Solution (Python):
from operator import itemgetter
import sys
def main():
    input = sys.stdin.readline
    n, m = map(int, input().split())
    a_b = [list(map(int, input().split())) for i in range(m)]
    a_b.sort(key=itemgetter(1))
    end = a_b[0][1]-2
    ans = 1 
    for i in range(m):
        if a_b[i][0]-1 > end:
            end = a_b[i][1]-2
            ans += 1
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
from operator import itemgetter
import sys
def main():
    input = sys.stdin.readline
    n, m = map(int, input().split())
    a_b = [list(map(int, input().split())) for i in range(m)]
    a_b.sort(key=itemgetter(1))
    end = a_b[0][1]-2
    ans = 1 
    for i in range(m):
        if a_b[i][0]-1 > end:
            end = a_b[i][1]-2
            ans += 1
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
from operator import itemgetter
import sys
def main():
    input = sys.stdin.readline
    n, m = map(int, input().split())
    a_b = [list(map(int, input().split())) for i in range(m)]
    a_b.sort(key=itemgetter(1))
    end = a_b[0][1]-2
    ans = 1 
    for i in range(m):
        if a_b[i][0]-1 <= end:
            continue
        else:
            end = a_b[i][1]-2
            ans += 1
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
n,m = map(int,input().split())
x=y=0
lst=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])
for a,b in lst:
  if a>x:
    y+=1
    x=b-1
print(y)

Solution (Python):
def solve(n, m, war):
    
    
    destroyed = -1
    ans = 0
    for b, a in war:
        if destroyed < a:
            destroyed = b-1
            ans += 1

    return ans


if __name__ == '__main__':
    n, m = map(int, input().split())
    war = []
    for _ in range(m):
        a, b = map(int, input().split())
        a, b = a-1, b-1
        war.append((b, a))
    war.sort()
    print(solve(n, m, war))

Solution (Python):
n,m=map(int,input().split())

l_target=[]
for _ in range(m):
    a,b=map(int,input().split())
    l_target.append((a,b))

l_target.sort(key=lambda x:x[1])

count=0
pre_pos=0
for target in l_target:
    if (target[0]<pre_pos<target[1]):
        continue
    
    count+=1
    pre_pos=target[1]-0.5

print(count)

Solution (Python):
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(M)]
AB.sort(key=lambda x:(x[1],x[0]))

ans = 0
rb = -1
for a,b in AB:
  if a>rb:
    ans += 1
    rb = b-1
print(ans)

Solution (Python):
from operator import itemgetter
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]
AB.sort(key=itemgetter(1))

ok = 0
ans = 0
for a, b in AB:
    if a >= ok:
        ans += 1
        ok = b
print(ans)

Solution (Python):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
ab = sorted(ab, key=lambda x:x[1])
deleted = ab[0][1]-1
ans = 1
for a, b in ab:
    if a <= deleted < b:
        pass
    else:
        deleted = b - 1
        ans += 1

    
print(ans)

Solution (Python):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
ab = sorted(ab, key=lambda x:x[1])
deleted = ab[0][1]-1
ans = 1
for a, b in ab:
    if a <= deleted < b:
        pass
    else:
        deleted = b - 1
        ans += 1

    
print(ans)

Solution (Python):
from sys import stdin

n,m = [int(x) for x in stdin.readline().rstrip().split()]
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(m)]

li.sort()

point = 1
lin = [1,n]
for i in range(m):
    lin = [max(lin[0],li[i][0]),min(lin[1],li[i][1])]
    if lin[1]-lin[0] <= 0:
        point += 1
        lin = li[i]
print(point)

Solution (Python):
N, M = list(map(int, input().split()))
R = sorted([tuple(map(int, input().split())) for _ in range(M)])

ir = 0
d = N+1
ans = 0
for i in range(1, N):
    while ir<len(R) and R[ir][0]<=i:
       	d = min(d, R[ir][1])
        ir+=1

    if d==i+1:
        d = N+1
        ans += 1

print(ans)

Solution (Python):

from operator import itemgetter
n, m = list(map(int,input().split()))

ab = sorted([tuple(map(int,input().split())) for i in range(m)], key = itemgetter(1))
removed = -1
ans = 0

for a,b in ab:
    if a > removed:
        removed = b-1
        ans += 1
        
print(ans)

Solution (Python):
def main():

    N, M = map(int, input().split())
    A = []
    for _ in range(M):
        a, b = map(int, input().split())
        A.append([a, b-1])

    A.sort()

    a, b = A[0]
    cnt = 1
    for i in range(1, M):
        if A[i][0] <= b:
            a = A[i][0]
            b = min(b, A[i][1])
        else:
            a, b = A[i]
            cnt += 1
    return cnt

if __name__ == '__main__':
    print(main())

Solution (Python):
N, M = map(int, input().split())
c = [list(map(int, input().split())) for i in range(M)]
c.sort()
r = 0
bridge = N
for i in range(M):
  a, b = c[M-1-i]
  if(a > bridge or bridge >= b):
    r += 1
    bridge = a
print(r)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    AB=[list(map(int,input().split())) for _ in range(m)]
    AB.sort(lambda x:(x[1],x[0]))
    cnt=0
    L,R=AB[0]
    for a,b in AB:
        L=max(L,a)
        if(L>=R):
            cnt+=1
            L=a
            R=b
    print(cnt+1)
resolve()

Solution (Python):

n,m=map(int,input().split())
lists=[]
for i in range(m):
    a,b=map(int,input().split())
    lists.append((a,b))
lists=sorted(lists,key=lambda x:(x[0],x[1]))

Uselist=[0 for i in range(n)]
for j in lists:
    if not Uselist[j[0]]:
        Uselist[j[0]]=j
    else:
        continue

uselist=[]
for j in Uselist:
    if j!=0:
        uselist.append(j)
counter=0
inf=0
maxi=10**15
length=len(uselist)
for j in range(length):
    inf=max(inf,uselist[j][0])
    maxi=min(maxi,uselist[j][1])
    if inf<maxi:
        pass
    elif inf>=maxi:
        counter+=1
        inf=maxi
        maxi=uselist[j][1]
print(counter+1)

Solution (Python):
n,m=map(int,input().split())
d=sorted([[int(x) for x in input().split()] for y in range(m)], key=lambda x:x[1])

c,e=0,0
for i in range(m):
  if d[i][0]>e:
    e=d[i][1]-1
    c+=1

print(c)

Solution (Python):
n, m = map(int, input().split())
ab_lis = []
for i in range(m):
    ab_lis.append(list(map(int, input().split())))
ab_lis_s = sorted(ab_lis, key = lambda x: x[1])
cnt = 0
endtime = 0
for i in range(m):
    if ab_lis_s[i][0] >= endtime:
        endtime = ab_lis_s[i][1]
        cnt += 1
print(cnt)

Solution (Python):
N,M = map(int,input().split(' '))
sections = [list(map(int,input().split(' '))) for _ in range(M) ]

'''
tmp_list = [sections[0]]
for i in range(1,len(sections)):
    for j in range(i):
        if sections[i] < tmp_list[j]:
            tmp_list.insert(j,sections[i])
            break
        if j == i -1:
            tmp_list.append(sections[i])
'''
min_end = 10000000000000000
sorted_list = sorted(sections)
cnt = 0
for section in sorted_list:
    if min_end <= section[0]:
        cnt += 1
        min_end = min_end = 10000000000000000
    min_end = min(min_end,section[1])
cnt += 1
print(cnt)

Solution (Python):
N, M = map(int, input().split())
from operator import itemgetter
bridges = []
for i in range(M):
    bridges.append(list(map(int, input().split())))
bridges.sort(key=itemgetter(1))
b_right = 0
cnt = 0
for a,b in bridges:
    if a > b_right:
        b_right = b - 1
        cnt += 1
print(cnt)

Solution (Python):
N, M = map(int, input().split())
X = []
for _ in range(M):
    l, r = map(int, input().split())
    X.append((l, r))
X = sorted(X, key = lambda x: x[1])

a = -1
ans = 0
for l, r in X:
    if l >= a:
        a = r
        ans += 1

print(ans)

Solution (Python):
def main():
    n,m = map(int,input().split())
    ab=[]
    for _ in range(m):
        ab.append(list(map(int,input().split())))
    ab.sort()
    Right=ab[0][1]
    cnt=1
    for i in range(m):
        if ab[i][1]<Right:
            Right=ab[i][1]
        if Right<=ab[i][0]:
            cnt+=1
            Right=ab[i][1]
    print(cnt)
if __name__ == "__main__":
    main()

Solution (Python):
n,m=map(int,input().split())
l=[]
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort()
s=l[0][1]
ans=1
for i in range(1,m):
    if l[i][0]>=s:
        ans+=1
        s=l[i][1]
    if l[i][1]<=s:
        s=l[i][1]
print(ans)

Solution (Python):
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(M)]
AB.sort()

ar=N+1
ans=0
for i in reversed(range(M)):
  a,b=AB[i]
  if b<=ar:
    ans+=1
    ar=a
  
print(ans)

Solution (Python):
import numpy as np
n,m=map(lambda x: int(x), input().split())
start=[]
end=[]
for i in range(m):
    a,b=map(lambda x: int(x), input().split())
    start.append(a)
    end.append(b)
start=np.array(start)
end=np.array(end)
ans=0
while len(start):
    ans+=1
    idx1=(end==end.min()) 
    idx2=(start<end.min()) 
    idx3=np.logical_or(idx1, idx2)
    idx4=np.logical_not(idx3) 
    start=start[idx4]
    end=end[idx4]
print(ans)

Solution (Python):
n,m=map(int,input().split())
points=[]
for i in range(m):
    a,b=map(int,input().split())
    points.append([a,b-1])
points = sorted(points)
arrow = float('-inf')
res = 0

for l,r in points:
    if l > arrow:
        res += 1
        arrow = r
    elif r < arrow:
        arrow = r
print(res)

Solution (Python):
import sys
from operator import itemgetter
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N, M = map(int, readline().split())
    ab = map(int, read().split())
    ab = sorted(zip(ab, ab), key=itemgetter(1))

    east_island = ab[0][1]
    answer = 1

    for i, j in ab:
        if i >= east_island:
            answer += 1
            east_island = j

    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from operator import itemgetter
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

N, M = map(int, readline().split())
ab = map(int, read().split())
ab = sorted(zip(ab, ab), key=itemgetter(1))

right_island = ab[0][1]
answer = 1

for i, j in ab:
    if i >= right_island:
        answer += 1
        right_island = j

print(answer)

Solution (Python):
import sys
from operator import itemgetter
readline = sys.stdin.readline
readlines = sys.stdin.readlines

N, M = map(int, readline().split())
ab = [tuple(map(int, i.split())) for i in readlines()]

ab.sort(key=itemgetter(1))
right_island = ab[0][1]
answer = 1

for i, j in ab:
    if i >= right_island:
        answer += 1
        right_island = j

print(answer)

Solution (Python):
from operator import itemgetter

n,m = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(m)]
ab.sort(key=itemgetter(1))
count = 0
R = 0
for a, b in ab:
     if a >= R:
            count += 1
            R = b
print(count)

Solution (Python):
n,m = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(m)]

AB.sort(reverse=1)

res_L = float('inf')
ans = 0
for a,b in AB:
	cur_R = b
	if cur_R <= res_L:
		res_L = a
		ans += 1
print(ans)

Solution (Python):
n , m = map(int,input().split())
war = []
for _ in range(m):
    a , b = map(int,input().split())
    war.append([a,b])
war.sort()
ans = 1
min_right = war[0][1]
max_left = war[0][0]
for i in range(0,m - 1):
    now_left, now_right = war[i][0], war[i][1]
    next_left , next_right = war[i + 1][0] , war[i + 1][1]
    min_right = min(min_right , next_right)
    max_left = max(max_left,next_left)
    if max_left >= min_right:
        ans += 1
        min_right = next_right
print(ans)

Solution (Python):
n, m = map(int, input().split())
a_and_b_list = [list(map(int, input().split())) for _ in range(m)]
bridge_broken = [False for _ in range(n-1)]
a_and_b_list.sort(key=lambda x: x[1])
previous_broken_bridge = -1
for a_and_b in a_and_b_list:
    if previous_broken_bridge < a_and_b[0]:
        bridge_broken[a_and_b[1]-2] = True
        previous_broken_bridge = a_and_b[1] - 1
print(bridge_broken.count(True))

Solution (Python):
from operator import itemgetter

N,M = map(int,input().split())
AB = sorted([list(map(int,input().split())) for _ in range(M)],key=itemgetter(1))
x, ans = AB[0][1],1
for a,b in AB:
  if a>=x:
    x = b
    ans += 1
print(ans)

Solution (Python):
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
ab.sort()
inf=float("inf")
t=inf
count=0
for i in range(m):
    a,b=ab[i]
    if a<t:
        t=min(t,b)
    else:
        count+=1
        t=b
count+=1
print(count)

Solution (Python):
from operator import itemgetter

n,m=map(int,input().split())
l=sorted([list(map(int,input().split())) for i in range(m)],key=itemgetter(1))
x,cnt=l[0][1],1
for i in l:
    a=i[0]
    b=i[1]
    if a>=x:
        cnt+=1
        x=b
print(cnt)

Solution (Python):
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
for i in l:
    i[0],i[1]=i[1],i[0]
ll=sorted(l)
x,cnt=ll[0][0],1
for i in ll:
    a=i[1]
    b=i[0]
    if a>=x:
        cnt+=1
        x=b
print(cnt)

Solution (Python):
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
for i in l:
    i[0],i[1]=i[1],i[0]
ll=sorted(l)
x,cnt=ll[0][0],1
for i in ll:
    a=i[1]
    b=i[0]
    if a>=x:
        cnt+=1
        x=b
print(cnt)

Solution (Python):
from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]
D = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
for d in D:
    if d[0] > d[1]:
        d[0], d[1] = d[1], d[0]

D.sort(key=lambda x: (x[0], x[1]))
left = -1
right = N
ans = 0

for d in D:
    left = max(d[0], left)
    right = min(d[1], right)
    if left >= right:
        ans += 1
        left = d[0]
        right = d[1]

print(ans + 1)

Solution (Python):
n, m = map(int, input().split())
bri_list=[0]
s = [[]]*m
for i in range(m):
    s[i] = list(map(int, input().split()))
s.sort(key=lambda x :x[1])

for i in range(m):
    if s[i][0]>bri_list[-1]:
        bri_list.append(s[i][1]-0.5)
print(len(bri_list)-1)

Solution (Python):
from operator import itemgetter

n, m = map(int, input().split())


ab = sorted([tuple(map(int, input().split())) for i in range(m)], key=itemgetter(1))

removed = -1
ans = 0

for a, b in ab:
    
    if a > removed:
        removed = b - 1
        ans += 1

print(ans)

Solution (Python):
N,M = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(M)]
l.sort(key= lambda x: x[1])
removed = 0
ans = 0
for i in l:
  s = i[1]
  if removed<i[0]:
    removed = s-1
    ans+=1

print(ans)

Solution (Python):

import sys

 
def main():
    N,M = map(int,input().split())
    ab = [list(map(int,input().split())) for _ in range(M)]
    ab.sort(key= lambda x: x[1])
    removed_bridge = 0
    answer = 0
    for aabb in ab:
        b = aabb[1]
        if removed_bridge<aabb[0]:
            removed_bridge = b-1
            answer+=1
    
    print(answer)
        



if __name__ == '__main__':
    main()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    v = LIR(m)
    v.sort(key = lambda x:x[1])
    ans = 0
    r = 0
    for a,b in v:
        if r < a:
            ans += 1
            r = b-1
        else:
            continue
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
n,m=map(int,input().split())
a=[tuple(map(int,input().split()))for i in range(m)]
a.sort()
l,r=0,n+1
x=1
for i,j in a:
  if j<=l or r<=i:
    x+=1
    l,r=i,j
  else:
    l,r=max(l,i),min(r,j)
print(x)

Solution (Python):
n, m = map(int, input().split( ))

war = [10**10 for i in range(n)]


for _ in range(m):
    a, b = map(int, input().split( ))
    war[a-1] = min(war[a-1],b-1)

a = []
b = []

for ai,bi in enumerate(war):
    if bi<10**9:
        a.append(ai)
        b.append(bi)


bridge = []
i=0
l = len(a)
while i<l:
    b_dum = b[i]
    while i+1<l and a[i+1]<b_dum:
        b_dum = min(b_dum,b[i+1])
        i+=1
    bridge.append(b_dum-1)
    i += 1    

print(len(bridge))

Solution (Python):


def resolve():
    (N,M)=map(int,input().split())
    querys=[[int(j) for j in input().split()] for i in range(M)]
    querys.sort()
    now=[querys[0][0],querys[0][1]]
    count=1

    for i in range(1,len(querys)):
        
        if querys[i][0]<now[1]:
            now[1]=min(now[1],querys[i][1])
            now[0]=querys[i][0]

        else:
            now=[querys[i][0],querys[i][1]]
            count+=1

    print(count)



if __name__ == "__main__":
    resolve()

Solution (Python):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]
AB = sorted(AB, key=lambda x: x[1])

ans = 0
m = 0
for ab in AB:
    if not ab[0] < m <= ab[1]:
        m = ab[1]
        ans += 1
print(ans)

Solution (Python):
N, M = [int(str) for str in input().strip().split()]
AB = [[int(str) for str in input().strip().split()] for _ in range(M)]

B_A = [[] for i in range(N + 1)]
for ab in AB:
    B_A[ab[1]].append(ab[0])
for b_a in B_A:
    b_a.sort()


right = 0
ans = 0
for i in range(N + 1):
    if len(B_A[i]):
        if B_A[i][-1] > right:
            ans += 1
            right = i - 1

print(ans)

Solution (Python):
def main():
    n, m = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(m)]
    D.sort()
    result = 1
    checker = D[0][1]
    for i in range(m):
        if checker <= D[i][0]:
            result += 1
            checker = D[i][1]
        elif checker > D[i][1]:
            checker = D[i][1]
    print(result)
if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(m)]
D.sort()
result = 1
checker = D[0][1]
for i in range(m):
    if checker <= D[i][0]:
        result += 1
        checker = D[i][1]
    elif checker > D[i][1]:
        checker = D[i][1]
print(result)

Solution (Python):
n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(m)]
ab.sort(key=lambda x: x[1])
ans = 0

br = -1
for i in range(m):
    if ab[i][0] < br < ab[i][1]:
        continue
    else:
        br = ab[i][1] - 0.5
        ans += 1

print(ans)

Solution (Python):
n, m = map(int, input().split())
requests = []
for _ in range(m):
    requests.append(tuple(map(int, input().split())))
requests.sort(key=lambda x: x[1])
cutcount = 0
lastcut = 0
for request in requests:
    if request[0] >= lastcut:
        lastcut = request[1]
        cutcount += 1
print(cutcount)

Solution (Python):
N, M = map(int,input().split())

banned_list = []
for i in range(M):
    banned_list.append(tuple(map(int,input().split())))
banned_list = sorted(banned_list,key=lambda x:x[1])

status = set()
counter = 0
for i in range(M):
    a = banned_list[i][0]
    b = banned_list[i][1]
    if len([one for one in status if (one >= a-1 and one < b-1)]) == 0:
        status  |= {b-2}
        counter += 1
print(counter)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from operator import itemgetter

N,M = map(int,readline().split())
m = map(int,read().split())
AB = sorted(zip(m,m),key=itemgetter(1))

answer = 0
x = 0
for a,b in AB:
    if a <= x:
        continue
    x=b-1
    answer += 1

print(answer)

Solution (Python):
N,M=map(int,input().split())
mp=[ tuple(map(int,input().split())) for _ in range(M)]

mp_=sorted(mp,key=lambda x: x[1])

ans=0
cut_bridge=0

for n in range(M):
    if mp_[n][0]>cut_bridge:
        ans+=1
        cut_bridge=mp_[n][1]-1
print(ans)

Solution (Python):
n,m = map(int,input().split())
youbou = [list(map(int,input().split())) for i in range(m)]
youbou.sort(key = lambda x: x[1])
ans,prev = 0,-1
for i in range(m):
	if prev < youbou[i][0]:
		prev = youbou[i][1] - 1
		ans += 1
print(ans)

Solution (Python):
n,m = map(int,input().split())
youbou = [list(map(int,input().split())) for i in range(m)]
youbou.sort(key = lambda x: x[1])
ans,prev = 0,-1
for p in youbou:
	if prev < p[0]:
		prev = p[1] - 1
		ans += 1
print(ans)

Solution (Python):
N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(M)]


ab = sorted(ab, key = lambda x: x[1])
tmp_end = ab[0][1]
n_remove_bridge = 1

for idx in range(1,M):
    if tmp_end <= ab[idx][0]:
        tmp_end = ab[idx][1]
        n_remove_bridge += 1

print(n_remove_bridge)

Solution (Python):
def main():
    N, M = (int(i) for i in input().split())
    ab = [tuple(int(i) for i in input().split()) for j in range(M)]

    dist = 1
    ans = 0
    ab.sort(key=lambda p: p[1])
    for a,b in ab:
        if dist <= a:
            ans += 1
            dist = b
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N,M = map(int,input().split())
l = [[0]*2 for i in range(M)]
sum = 0
for i in range(M):
    l[i][0],l[i][1] = map(int,input().split())
l.sort(key=lambda l:l[1])
x = 0
for i in range(M):
    if x<=l[i][0]:
        x=l[i][1]
        sum+=1
print(sum)

Solution (Python):
n,m=map(int,input().split())
l=list(list(map(int,input().split())) for i in range(m))
l=sorted(l, key=lambda x:x[1])

ans=0
count=0

for i in range(m):
  if count<l[i][0]:
    ans+=1
    count=l[i][1]-1
print(ans)

Solution (Python):
N, M = map(int, input().split())
bridges = [list(map(lambda x: int(x)-1, input().split())) for i in range(M)]
l = [None for i in range(N)]
for a, b in bridges:
  if l[b] is None:
    l[b] = a
  else:
    l[b] = max(l[b], a)

now = 0
res = 0
for dist in range(N):
  src = l[dist]
  if src is None:
    continue
  if src >= now:
    now = dist
    res += 1

print(res)

Solution (Python):
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
ab=sorted(ab, key=lambda x:x[1])
ans=0
bridge=0
for i in range(m):
    if bridge<ab[i][0]:
        ans+=1
        bridge=ab[i][1]-1
print(ans)

Solution (Python):
n, m = map(int, input().split())
count = 0
s = [[]*2]*m
for i in range(0, m):
  s[i] = list(map(int, input().split()))
s.sort()
r = s[0][1] - 1
count += 1
for i in range(1, m):
  if s[i][1] - 1 < r:
    r = s[i][1] - 1
  if r < s[i][0]:
    r = s[i][1] - 1
    count += 1
print(count)

Solution (Python):
n, m = map(int, input().split())
count = 0
s = [[]*2]*m
for i in range(0, m):
  s[i] = list(map(int, input().split()))
s.sort()
r = s[0][1] - 1
count += 1
for i in range(1, m):
  if s[i][1] < r + 1:
    r = s[i][1] - 1
  if s[i][0] > r:
    r = s[i][1] - 1
    count += 1
print(count)

Solution (Python):
n,m = map(int,input().split())
war = [list(map(int,input().split())) for i in range(m)]
war.sort(key=lambda x: x[1])
broken = 0
ans = 0
for i in range(m):
   if war[i][0] > broken:
       ans += 1
       broken = war[i][1]-1
print(ans)

Solution (Python):
N,M=map(int,input().split())
hope=[tuple([int(i) for i in input().split()]) for j in range(M)]
hope.sort(key=lambda x:x[1])
ans=0
prebroke=0
for s,g in hope:
    if s<=prebroke:
        continue
    else:
        ans+=1
        prebroke=g-1
print(ans)

Solution (Python):


N, M = map(int, input().split()) 
ab = [list(map(int, input().split())) for i in range(M)] 
ans = 0 
left = 1 
ab.sort(key = lambda x: x[1]) 
for request in ab:
    if left <= request[0]: 
        ans += 1
        left = request[1] 
print(ans)

Solution (Python):
N, M = map(int, input().split())
AB = []
for _ in range(M):
    AB.append(list(map(int, input().split())))
AB.sort(key=lambda x: x[1])

ans = 1
removed = AB[0][1] - 1
for i in range(M):
    if AB[i][0] > removed:
        removed = AB[i][1] - 1
        ans += 1
print(ans)

Solution (Python):
import heapq

n, m = [ int(v) for v in input().split() ]
demand = []
for i in range(m):
    l, r = [ int(v) for v in input().split() ]
    demand.append( (r,l) )
demand.sort()

right, left = heapq.heappop(demand)
ans = 1

for i in range(m-1):
    ri, li = heapq.heappop(demand)
    if li >= right:
        ans += 1
        right = ri

print(ans)

Solution (Python):
n, m = map(int, input().split())
spans = sorted([list(map(int, input().split())) for _ in range(m)], key=lambda x: x[1])
cnt = 0
x = 0

for span in spans:
    if span[0] > x:
        x = span[1] - 1
        cnt += 1

print(cnt)

Solution (Python):
from heapq import heappush, heappop
N, M = map(int, input().split())

ab = [[int(i) for i in input().split()] for _ in range(M)]

ab.sort(key=lambda x: x[1])
ab.sort()

ret = 1
end = float('inf')
for a, b in ab :
    if a >= end :
        ret += 1
        end = b
    else :
        end = min(end, b)

print(ret)

Solution (Python):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(m)]

AB.sort(key=lambda x:(x[1],x[0]))
end=AB[0][1]
ans=1
for a,b in AB[1:]:
  if end<=a:
    end=b
    ans+=1
print(ans)

Solution (Python):
def run():
  N, M = map(int, input().split())
  land_li = [list(map(int, input().split())) for m in range(M)]
  sorted_li = sorted(land_li, key=lambda x:(x[1], x[0]))
  
  end = sorted_li[0][1]
  num = 1
  for batle_li in sorted_li[1:]:
    if end <= batle_li[0]:
      end = batle_li[1]
      num += 1
  print(num)
  
if __name__ == '__main__':
  run()

Solution (Python):
n,m = map(int,input().split())
ab = []
for _ in range(m):
  ab.append(list(map(int,input().split())))
ab.sort(key = lambda x:x[1])

bridge = [1]*(n-1)
x = -1
for a_b in ab:
  if a_b[0] <= x:
    pass
  else:
    x = a_b[1]-1
    bridge[x-1]=0

print(bridge.count(0))

Solution (Python):
def log(s):
    
    pass


def output(x):
    print(x, flush=True)


def input_ints():
    return map(int, input().split())


def main():
    num_nodes, num_regions = tuple(input_ints())
    regions = [tuple(input_ints()) for _ in range(num_regions)]
    regions.sort(key=lambda r: r[1])

    edges = []
    for r in regions:
        if not edges or edges[-1] < r[0]:
            edges.append(r[1] - 1)

    print(len(edges))


main()

Solution (Python):
N, M = map(int, input().split())
C = []
for i in range(M):
    C.append(list(map(int, input().split())))
C.sort(key=lambda x:x[0])
ans = 1
l = 0
r = N-1
for c in C:
    if r<=c[0]-1 or c[1]-1<=l:
        ans += 1
        l = c[0]-1
        r = c[1]-1
        continue
    l = max(l, c[0]-1)
    r = min(r, c[1]-1)
print(ans)

Solution (Python):
from operator import itemgetter

N, M = map(int, input().split())
AB = sorted([tuple(map(int, input().split())) for _ in range(M)], key=itemgetter(1))

removes = -1
answer = 0

for a, b in AB:
    if a > removes:
        removes = b - 1
        answer += 1
print(answer)