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



ab = [[] for _ in range(N + 1)]

for _ in range(M):

    a, b = list(map(int, input().split()))

    ab[a].append(b)



dp = [0] * (N + 1)

for i in range(1, N + 1):

    dp[i] = max(dp[i], dp[i - 1])

    for j in ab[i]:

        dp[j] = max(dp[j], dp[i] + 1)



print((dp[N]))

from sys import stdin

readline = stdin.readline



N, M = list(map(int, readline().split()))

ab = [list(map(int, readline().split())) for _ in range(M)]



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



result = 0

t = -1

for a, b in ab:

    a, b = a - 1, b - 1

    if a <= t < b:

        continue

    result += 1

    t = b - 1

print(result)

N, M = list(map(int, input().split()))
ab = [[] for _ in range(N + 1)]
for _ in range(M):
    a, b = list(map(int, input().split()))
    ab[a].append(b)
dp = [0] * (N + 1)
for i in range(1, N + 1):
    dp[i] = max(dp[i], dp[i - 1])
    for j in ab[i]:
        dp[j] = max(dp[j], dp[i] + 1)
print((dp[N]))

from sys import stdin

readline = stdin.readline
N, M = list(map(int, readline().split()))
ab = [list(map(int, readline().split())) for _ in range(M)]
ab.sort(key=lambda x: x[1])
result = 0
t = -1
for a, b in ab:
    a, b = a - 1, b - 1
    if a <= t < b:
        continue
    result += 1
    t = b - 1
print(result)

-N, M = list(map(int, input().split())) -ab = [[] for _ in range(N + 1)] -for _ in range(M): -    a, b = list(map(int, input().split())) -    ab[a].append(b) -dp = [0] * (N + 1) -for i in range(1, N + 1): -    dp[i] = max(dp[i], dp[i - 1]) -    for j in ab[i]: -        dp[j] = max(dp[j], dp[i] + 1) -print((dp[N])) +from sys import stdin + +readline = stdin.readline +N, M = list(map(int, readline().split())) +ab = [list(map(int, readline().split())) for _ in range(M)] +ab.sort(key=lambda x: x[1]) +result = 0 +t = -1 +for a, b in ab: +    a, b = a - 1, b - 1 +    if a <= t < b: +        continue +    result += 1 +    t = b - 1 +print(result)