from operator import itemgetter



n, m = [int(i) for i in input().split()]

A, B = list(zip(*[[int(i) for i in input().split()] for j in range(m)]))



A, B = list(zip(*sorted(zip(A, B), key=itemgetter(1, 0))))



last = -1

ans = 0



for a, b in zip(A, B):

    if last <= a:

        ans += 1

        last = b



print(ans)
n, m = [int(i) for i in input().split()]

A = [[int(i) for i in input().split()] for j in range(m)]



A.sort(key=lambda a:a[1])

ans = 0

i = 0



while i < m:

    b = A[i][1]

    ans += 1

    i += 1

    while i < m:

        if A[i][0] >= b:

            break

        i += 1



print(ans)
from operator import itemgetter

n, m = [int(i) for i in input().split()]
A, B = list(zip(*[[int(i) for i in input().split()] for j in range(m)]))
A, B = list(zip(*sorted(zip(A, B), key=itemgetter(1, 0))))
last = -1
ans = 0
for a, b in zip(A, B):
    if last <= a:
        ans += 1
        last = b
print(ans)

n, m = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()] for j in range(m)]
A.sort(key=lambda a: a[1])
ans = 0
i = 0
while i < m:
    b = A[i][1]
    ans += 1
    i += 1
    while i < m:
        if A[i][0] >= b:
            break
        i += 1
print(ans)

-from operator import itemgetter - -A, B = list(zip(*[[int(i) for i in input().split()] for j in range(m)])) -A, B = list(zip(*sorted(zip(A, B), key=itemgetter(1, 0)))) -last = -1 +A = [[int(i) for i in input().split()] for j in range(m)] +A.sort(key=lambda a: a[1]) -for a, b in zip(A, B): -    if last <= a: -        ans += 1 -        last = b +i = 0 +while i < m: +    b = A[i][1] +    ans += 1 +    i += 1 +    while i < m: +        if A[i][0] >= b: +            break +        i += 1