CodeEff/ECCO huggingface dataset:
# Original solution
# -*- coding: utf-8 -*-
from operator import itemgetter

n,m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
ab.sort(key=itemgetter(1,0))

cnt = 0
cb = -1
for (a,b) in ab:
    if cb == -1:
        cb = b - 1
        cnt += 1
    elif a<=cb:
        continue
    else:
        cb = b - 1
        cnt += 1

print(cnt)


# Optimized solution
# -*- coding: utf-8 -*-
from operator import itemgetter
n,m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]

res = 0
ab.sort(key=itemgetter(1))

i = 0
while i<m:
    a,b = ab[i]
    for j in range(i+1,m):
        if ab[j][0]<b<=ab[j][1]:
            i += 1
        else:
            break
    i += 1
    res += 1

print(res)
# Original solution
n,m = list(map(int,input().split()))
li = [list(map(int,input().split())) for _ in range(m)]

ans = 0
now = -1

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

for i in range(m):
    if li[i][0] <= now and li[i][1] > now:
        pass
    else:
        ans += 1
        now = li[i][1] - 1

print(ans)


# Optimized solution
import sys
input = sys.stdin.readline

n,m = list(map(int,input().split()))
li = [list(map(int,input().split())) for _ in range(m)]

ans = 0
now = -1

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

for i in range(m):
    if li[i][0] <= now and li[i][1] > now:
        pass
    else:
        ans += 1
        now = li[i][1] - 1

print(ans)
# Original solution
N,M=list(map(int,input().split()))
E=[(1,N)]
for _ in[0]*M:
    a,b=list(map(int,input().split()))
    E+=[(a,b)]
    F=[]
    for s,t in E:
        if s<=a and b<=t:
            F+=[(a,b)]
        else:
            F+=[(s,t)]
    E=F
cnt=0
while E:
    cnt+=1
    M, Mi = 0, 0
    A = [0]*N
    l = len(E)
    B = [set(range(l))for _ in[0]*N]
    for k,(s,t) in enumerate(E):
        for i in range(s,t):
            A[i]+=1
            B[i]-={k}
            if M<A[i]:
                M, Mi = A[i], i
    E=[E[i]for i in B[Mi]]
print(cnt)

# Optimized solution
n,m=list(map(int,input().split()))
ab=sorted([list(map(int,input().split()))for _ in[0]*m],key=lambda x:x[1])
cnt=0
x=0
for a,b in ab:
   if x<a:x=b-1;cnt+=1
print(cnt)
# Original solution
import sys
input=sys.stdin.readline
n,m=list(map(int,input().split()))
AB=[list(map(int,input().split())) for _ in range(m)]

AB.sort(key=lambda x:x[1])
bridge=[True]*(n-1)
for a,b in AB:
  if bridge[a-1:b-1].count(False)==0:
    bridge[b-2]=False
print((bridge.count(False)))

# Optimized solution
import sys
input=sys.stdin.readline
n,m=list(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)
# Original solution
N,M = list(map(int, input().split()))
AB = []
for _ in range(M):
    AB.append(list(map(int,input().split())))

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

cur = 0
ans = 0
for a,b in AB:
    if cur > a:
        continue
    
    ans += 1
    cur = b

print(ans)

# Optimized solution
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()
ab = []
for _ in range(m):
    a,b = li_()
    ab.append(tuple([a,b]))
    
ab.sort(key=lambda x:x[1])
ans = 0
cur = -1

for a,b in ab:
    if a >= cur:
        ans += 1
        cur = b
        
print(ans)
# Original solution
N, M = list(map(int, input().split()))
lst = [tuple(map(int, input().split())) for _ in range(M)]
lst.sort(key=lambda x: x[1])
bridge = [False for _ in range(N - 1)]
for war in lst:
    if True not in bridge[war[0] - 1:war[1] - 1]:
        bridge[war[1] - 2] = True
print((bridge.count(True)))

# Optimized solution
N, M = list(map(int, input().split()))
lst = [tuple(map(int, input().split())) for _ in range(M)]
lst.sort(key=lambda x: x[1])
bridge = []
for war in lst:
    if bridge and war[0] <= bridge[-1]:
        continue
    bridge.append(war[1] - 1)
print((len(bridge)))
# Original solution
def main():
	n, m = list(map(int, input().split()))
	rqs = [list(map(int, input().split())) for _ in range(m)]

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

	cnt = 0
	while len(rqs) > 1:
		rqs = [x for x in rqs[1:] if x[0] >= rqs[0][1]]
		cnt += 1
		#print(rqs, cnt)

	if len(rqs) == 1:
		cnt += 1

	print(cnt)

if __name__ == '__main__':
	main()

# Optimized solution
def main():
	n, m = list(map(int, input().split()))
	rqs = [list(map(int, input().split())) for _ in range(m)]

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

	b = -1
	cnt = 0
	
	for x in rqs:
		if b < x[0]:
			b = x[1] - 1
			cnt += 1

	print(cnt)

if __name__ == '__main__':
	main()
# Original solution
from operator import itemgetter
N, M = list(map(int, input().split()))
ab = [[0,0] for i in range(M)]
for i in range(M):
    ab[i] = list(map(int, input().split()))

ab = sorted(ab, key=itemgetter(0,1))

re = 1
x, y = ab[0]
for i in range(1, M):
    a, b = ab[i]
    if b <= x or y <= a: # 切ったとこの範囲外だったら
        re += 1
        x, y = a, b
    else:
        x = max(x, 1)
        y = min(y, b)
print(re)

# Optimized solution
N, M = list(map(int, input().split()))

pair = []
for i in range(M):
    a, b = list(map(int, input().split()))
    pair.append([b, a])

# bをキーにして昇順にソートする
pair.sort()

ans = 1
y, x = pair[0]

for i in range(1, M):
    b, a = pair[i]
    if y <= a:
        y = b
        ans += 1

print(ans)
# Original solution
n,m,*t=list(map(int,open(0).read().split()))
c=d=0
for b,a in sorted([(b,a)for a,b in zip(*[iter(t)]*2)]):
	if a>d:c+=1;d=b-1
print(c)

# Optimized solution
n,m,*t=list(map(int,open(0).read().split()));r=[n+1]*(n+1)
for a,b in zip(*[iter(t)]*2):
	r[a]=min(r[a],b)
a=0;cur=n+1
for i in range(n+1):
	cur=min(cur, r[i])
	if i==cur:
		cur=r[i]
		a+=1
print(a)
# Original solution
import sys
n, m = list(map(int, sys.stdin.readline().split()))
req = [ list(sorted(map(int, sys.stdin.readline().split())))
        for _ in range(m) ]
req.sort(reverse=True)
cutlist = []
for a, b in req:
    cut = False
    for i in cutlist:
        if a <= i and i < b:
            cut = True
            break
    if not cut:
        cutlist.append(a)
print((len(cutlist)))


# Optimized solution
import sys
n, m = list(map(int, sys.stdin.readline().split()))
req = [ list(sorted(map(int, sys.stdin.readline().split())))
        for _ in range(m) ]
req.sort(reverse=True)
lastcut = None
c = 0
for a, b in req:
    if lastcut is None or a > lastcut or lastcut >= b:
        lastcut = a
        c += 1
print(c)
# Original solution
import sys,queue,math,copy,itertools,bisect,collections,heapq
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

N,M = LI()
island = [0 for _ in range(N)]
req = [[] for _ in range(N)]
for _ in range(M):
    a,b = LI()
    req[a-1].append(b-1)

p = 1
ans = 0
for i in range(N):
    if island[i] > p:
        ans += 1
        p = island[i]
    for b in req[i]:
        island[b] = p+1
print (ans)




# Optimized solution
import sys
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

N,M = LI()
island = [0 for _ in range(N)]
req = [[] for _ in range(N)]
for _ in range(M):
    a,b = LI()
    req[a-1].append(b-1)

p = 1
ans = 0
for i in range(N):
    if island[i] > p:
        ans += 1
        p = island[i]
    for b in req[i]:
        island[b] = p+1
print(ans)
# Original solution
import bisect

N,M = [int(x) for x in input().split()]

l = [[int(x) for x in input().split()] for _ in range(M)]



def do(l):
    

    count = 1

    l.sort()

    while True:

        # l2 = [v2 for (_,v2) in l]
        # l2.sort()
        

        nx = min(l,key=lambda x:x[1])[1]

        for i in range(len(l)):
            #print(l)
            
            if l[i][0] >= nx:
                #print("i")
                #print(i)
                #print(count)
                # h+=1
                # if i > 0:
                l = l[i:]
                count+=1
                break
            if i >= len(l)-1:
                return count
        if l == []:
            return count



result = do(l)
print(result)

# Optimized solution
import bisect

N,M = [int(x) for x in input().split()]

l = [[int(x) for x in input().split()] for _ in range(M)]



def do(l):

    count = 1

    l.sort()
    right = l[0][1]


    for i in range(len(l)):
        if l[i][0] >= right:
            right = l[i][1]
            count+=1
            continue
        if l[i][1] < right:
            right = l[i][1]
    return count

testl = [[x,x+1] for x in range(1)]

result = do(l)
print(result)
# Original solution
import copy
n,m = list(map(int,input().split()))
ab = [tuple(map(int,input().split())) for _ in range(m)]
bridge = [0 for _ in range(n)]
bridge_m = [[] for _ in range(n)]
for i in range(m):
    for j in range(ab[i][0],ab[i][1]):
        bridge[j] +=1
        bridge_m[j].append(i)
cnt =0
max_ = max(bridge)
while(max_!=0):
    argmax = bridge.index(max_)
    delete = copy.deepcopy(bridge_m[argmax])
    for i in delete:
        for j in range(ab[i][0],ab[i][1]):
            bridge[j] -=1
            bridge_m[j].remove(i)
    cnt +=1
    max_ =max(bridge)
print(cnt)

# Optimized solution
n,m = list(map(int,input().split()))
ab = [tuple(map(int,input().split())) for _ in range(m)]
ab.sort(key=lambda x:x[1])
last=-1
cnt=0
for i in range(len(ab)):
    if(ab[i][0]<=last):pass
    else:
        last=ab[i][1]-1
        cnt+=1
print(cnt)
# Original solution
N, M = list(map(int, input().split()))
ab = []
for _ in range(M):
    a, b = list(map(int, input().split()))
    ab.append((a,b))

# bの昇順でsort
ab.sort(key=lambda inv: inv[1])   


count = 0
while(0 < len(ab)):
    count += 1
    cutb = ab[0][1]
    # cutbで除外できる闘争を除外する
    remove_list = []
    for i in range(len(ab)):
        a, b = ab[i]
        if a <= cutb - 1 and cutb <= b:
            remove_list.append(i)

    for remove_index in reversed(remove_list):
        ab.pop(remove_index)

print(count)


# Optimized solution
N, M = list(map(int, input().split()))
ab = []
for _ in range(M):
    a, b = list(map(int, input().split()))
    ab.append((a,b))

# bの昇順でsort
ab.sort(key=lambda inv: inv[1])   


count = 0
while(0 < len(ab)):
    count += 1
    cutb = ab[0][1]
    # cutbで除外できる闘争を除外する
    remove_index = 0
    for i in range(len(ab)):
        a, b = ab[i]
        if a <= cutb - 1 and cutb <= b:
            remove_index = i
        else:
            break

    ab = ab[remove_index+1:]

print(count)
# Original solution
N, M = list(map(int, input().split()))
req = [tuple(map(int, input().split())) for _ in range(M)]
req.sort(key=lambda x: x[1])

res = 0
i = 1
while i <= N:
    remain = [x for x in req if i <= x[0]]
    if len(remain):
        i = remain[0][1]
        res += 1
    else:
        break

print(res)


# Optimized solution
N, M = list(map(int, input().split()))
req = [tuple(map(int, input().split())) for _ in range(M)]
req.sort(key=lambda x: x[1])

res = 0
i = 1
for m in range(M):
    if req[m][0] >= i:
        i = req[m][1]
        res += 1

print(res)
# Original solution
n,m=list(map(int,input().split()))
itv=[]
for i in range(m):
  a,b=list(map(int,input().split()))
  start=min(a,b)
  end=max(a,b)
  itv.append([end,start])
t,ans=0,0
itv=sorted(itv)
for e,s in itv:
  if t<=s:
    t=e
    ans+=1
print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
itv=[]
for i in range(m):
  a,b=list(map(int,input().split()))
  itv+=[(b,a)]
itv=sorted(itv)
cnt=0
l=1
for t,s in itv:
  if l<=s:
    l=t
    cnt+=1
print(cnt)
# Original solution
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
 
N, M = mi()
Q = li2(M)
Q = sorted(Q, key=lambda x:x[1])
 
table = dp2(0, N, N)
 
#for i in range(M):
    #s, t = mi()
    #for i in range(t):
    #table[s-1][t-1] = 1
 
#for i in reversed(range(N-1)):
    #for j in range(i, N):
        #table[i][j] = max(table[i+1][j], table[i][j])
 
#print(table)
'''
i = j = 0
cnt = 0
while True:
    while j < N and table[i][j] == 0:
        j += 1
    if j >= N:
        break
    cnt += 1
    i = j
 
print(cnt)
'''
 
l = [N]*N
for i in range(M):
    ind = max(0, Q[i][0]-1)
    l[ind] = min(Q[i][1]-1, l[ind])
 
for i in reversed(list(range(N-1))):
    l[i] = min(l[i], l[i+1])
 
cnt = 0
i = 0
#print(l)
while True:
    i = l[i]
    cnt += 1
    if l[i] == N:
        break
 
print(cnt)

# Optimized solution
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
 
N, M = mi()
Q = li2(M)
Q = sorted(Q, key=lambda x:x[1])
 
#table = dp2(0, N, N)
 
#for i in range(M):
    #s, t = mi()
    #for i in range(t):
    #table[s-1][t-1] = 1
 
#for i in reversed(range(N-1)):
    #for j in range(i, N):
        #table[i][j] = max(table[i+1][j], table[i][j])
 
#print(table)
'''
i = j = 0
cnt = 0
while True:
    while j < N and table[i][j] == 0:
        j += 1
    if j >= N:
        break
    cnt += 1
    i = j
 
print(cnt)
'''
 
l = [N]*N
for i in range(M):
    ind = max(0, Q[i][0]-1)
    l[ind] = min(Q[i][1]-1, l[ind])
 
for i in reversed(list(range(N-1))):
    l[i] = min(l[i], l[i+1])
 
cnt = 0
i = 0
#print(l)
while True:
    i = l[i]
    cnt += 1
    if l[i] == N:
        break
 
print(cnt)
# Original solution
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
 
N, M = mi()
Q = li2(M)
Q = sorted(Q, key=lambda x:x[1])
 
#table = dp2(0, N, N)
 
#for i in range(M):
    #s, t = mi()
    #for i in range(t):
    #table[s-1][t-1] = 1
 
#for i in reversed(range(N-1)):
    #for j in range(i, N):
        #table[i][j] = max(table[i+1][j], table[i][j])
 
#print(table)
'''
i = j = 0
cnt = 0
while True:
    while j < N and table[i][j] == 0:
        j += 1
    if j >= N:
        break
    cnt += 1
    i = j
 
print(cnt)
'''
 
l = [N]*N
for i in range(M):
    ind = max(0, Q[i][0]-1)
    l[ind] = min(Q[i][1]-1, l[ind])
 
for i in reversed(list(range(N-1))):
    l[i] = min(l[i], l[i+1])
 
cnt = 0
i = 0
#print(l)
while True:
    i = l[i]
    cnt += 1
    if l[i] == N:
        break
 
print(cnt)

# Optimized solution
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, M = mi()
Q = li2(M)
'''
l = [N]*N
for i in range(M):
    ind = max(0, Q[i][0]-1)
    l[ind] = min(Q[i][1]-1, l[ind])

for i in reversed(range(N-1)):
    l[i] = min(l[i], l[i+1])

cnt = 0
i = 0

while True:
    i = l[i]
    cnt += 1
    if l[i] == N:
        break

print(cnt)
'''

dp = [0]*N
#rel = [[] for _ in range(N)]
rel = [N]*N
for s, t in Q:
    rel[s-1] = min(t-1, rel[s-1])

#dp[0] = 0
for i in range(N-1):
    dp[i+1] = max(dp[i], dp[i+1])
    if rel[i] != N:
        dp[rel[i]] = max(dp[i] + 1, dp[rel[i]])

print((dp[N-1]))
# Original solution
n,m = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(m)]
ab.sort()

l,r = ab[0][0],ab[0][1]-1

ans = 1

for i in range(1,m):
    l = max(l,ab[i][0])
    r = min(r,ab[i][1]-1)
    if l > r:
        ans += 1
        l,r = ab[i][0],ab[i][1]-1

print(ans)

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

ans = 1
t = AB[0][1]-1

for i in range(M):
    if t < AB[i][0]:
        t = AB[i][1]-1
        ans += 1
print(ans)
# Original solution
n,m = list(map(int, input().split()))
t = [[n] for i in range(n)]
for i in range(m):
  a,b = list(map(int, input().split()))
  t[a-1].append(b-1)
g = []
for i in t:
  g.append(sorted(i))
k = 0
ans=0
while k <=n-1:
  e = n
  for i in g[k:]:
    e = min(e,i[0])
  if e==n:
    break
  ans+=1
  k = e
print(ans)

# Optimized solution
n,m = list(map(int, input().split()))
t = [[n] for i in range(n)]
for i in range(m):
  a,b = list(map(int, input().split()))
  t[a-1].append(b-1)
g = []
for i in t:
  g.append(min(i))
k = 0
ans=0
while k <=n-1:
  e = min(g[k:])
  if e==n:
    break
  ans+=1
  k = e
print(ans)
# Original solution
N,M = list(map(int,input().split()))
src = [tuple([int(x)-1 for x in input().split()]) for i in range(M)]
src.sort()

ans = 0
minb = N
for a,b in src:
    if a >= minb:
        ans += 1
        minb = b
    else:
        minb = min(minb,b)
if src[-1][0] < minb:
    ans += 1
print(ans)

# Optimized solution
N,M = list(map(int,input().split()))
src = [tuple(map(int,input().split())) for i in range(M)]

ans = 0
cut = -1
src.sort(key=lambda x:x[1])
for a,b in src:
    if a < cut: continue
    cut = b
    ans += 1
print(ans)
# Original solution
from operator import itemgetter

n, m = list(map(int, input().split()))
ab = sorted([list(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)

# Optimized solution
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)
# Original solution
# islands war
n,m=list(map(int,input().split()))
lists=[]
for i in range(m):
    a,b=list(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))

# Optimized solution
# atcoder -take a rest-(easy)

def main():
    N, M = list(map(int, input().split()))
    conflict = [tuple(map(int, input().split())) for i in range(M)]
    newest_destroyed = -1
    conflict.sort(key=lambda x: x[1])
    res = 0
    for l, r in conflict:
        if not (l <= newest_destroyed < r):
            res += 1
            newest_destroyed = r-1
    print(res)
    return


if __name__ == "__main__":
    main()
# Original solution
# atcoder -take a rest-(easy)

def main():
    N, M = list(map(int, input().split()))
    conflict = [tuple(map(int, input().split())) for i in range(M)]
    newest_destroyed = -1
    conflict.sort(key=lambda x: x[1])
    res = 0
    for l, r in conflict:
        if not (l <= newest_destroyed < r):
            res += 1
            newest_destroyed = r-1
    print(res)
    return


if __name__ == "__main__":
    main()




# Optimized solution
# atcoder -take a rest-(easy)
#typical greedy
def main():
    N, M = list(map(int, input().split()))
    conflict = [tuple(map(int, input().split())) for i in range(M)]
    newest_destroyed = -1
    conflict.sort(key=lambda x: x[1])
    res = 0
    for l, r in conflict:
        if not (l <= newest_destroyed < r):
            res += 1
            newest_destroyed = r-1
    print(res)
    return


if __name__ == "__main__":
    main()
# Original solution
N,M = list(map(int,input().split()))
Map = [[] for i in range(N)]
for i in range(M):
    a,b=list(map(int,input().split()))
    Map[a-1].append(b-1)

b_list = []
count=0   
for i in range(N):
    if i in b_list:
       b_list=[]
       count+=1
    b_list=b_list+Map[i]

print(count)

# Optimized solution

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

b_list = [0 for i in range(N)]
count=0   
for i in range(N):
    if b_list[i]==1:
        b_list=[0 for i in range(N)]
        count+=1
    for j in Map[i]:
        b_list[j]=1

print(count)
# Original solution
import heapq
N, M = list(map(int, input().split()))
data = []
for i in range(M):
    a, b = list(map(int, input().split()))
    heapq.heappush(data, (b, a))
bridge = [1] * N

for i in range(M):
    b, a = heapq.heappop(data)
    if sum(bridge[a:b]) == b - a:
        bridge[b-1] = 0

print((N-sum(bridge)))

# Optimized solution
import heapq
N, M = list(map(int, input().split()))
data = []
for i in range(M):
    a, b = list(map(int, input().split()))
    heapq.heappush(data, (b, a))

cut = 0
answer = 0
for i in range(M):
    b, a = heapq.heappop(data)
    if cut <= a:
        answer += 1
        cut = b

print(answer)
# Original solution
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
ab = sorted(ab, key = lambda x : x[1])

ans = 0
end = 0
for i in range(m):
    if ab[i][0] >= end:
        end = ab[i][1]
        ans += 1

print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]

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

ans = m
for i in range(1, m):
    if ab[i][0] < ab[i-1][1]:
        ab[i][1] = ab[i-1][1]
        ans -= 1
print(ans)
# Original solution
N, M = list(map(int,input().split()))
m=[list(map(int, input().split())) for _ in range(M)]
m=sorted(m, key=lambda x: x[1])
p=0
for _ in range(M):
    if m!=[]:
        tmp=max(m)[0]
        m=[l for l in m if l[1]<=tmp]
        p+=1
    else:
        break
print(p)

# Optimized solution
N, M = list(map(int,input().split()))
m=[list(map(int, input().split()))[::-1] for _ in range(M)]
m.sort()

b=[]
for i in m:
    if len([tmp for tmp in b if tmp>=i[1]])==0:
        b.append(i[0]-1)
print((len(b)))
# Original solution
import copy
N,M=list(map(int,input().split()))

bridge=[0]*(N-1)
l=[]
for _ in range(M):
  l.append(list(map(int,input().split())))

for sl in l:
  for i in range(sl[0]-1,sl[1]-1):
    bridge[i]+=1
ans=0
# bridge=[4,6,6,4]
while max(bridge)>=1:
  m=bridge.index(max(bridge)) #m=1
  l1=copy.deepcopy(l)
  for sl in l1:
    if(m>=sl[0]-1 and m<sl[1]-1):
      for j in range(sl[0]-1,sl[1]-1):
        bridge[j]-=1
      l.remove(sl)
  ans+=1
print(ans)

# Optimized solution
N,M = list(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)