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

events = []

for i in range(m):

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

    events.append((a, 0, i))

    events.append((b-1, 1, i))

events.sort()

solved = [False] * m

todo = []

ans = 0

for i, etype, req in events:

    if etype == 0:

        todo.append(req)

    else:

        if not solved[req]:

            ans += 1

            for req_id in todo:

                solved[req_id] = True

            todo = []

print(ans)
n, m = list(map(int, input().split()))

x = [-1] * (n+1)

for _ in range(m):

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

    x[b] = max(x[b], a)

ans = 0

bmx = 0

for i in range(n+1):

    if bmx <= x[i]:

        ans += 1

        bmx = i

print(ans)
n, m = list(map(int, input().split()))
events = []
for i in range(m):
    a, b = list(map(int, input().split()))
    events.append((a, 0, i))
    events.append((b - 1, 1, i))
events.sort()
solved = [False] * m
todo = []
ans = 0
for i, etype, req in events:
    if etype == 0:
        todo.append(req)
    else:
        if not solved[req]:
            ans += 1
            for req_id in todo:
                solved[req_id] = True
            todo = []
print(ans)

n, m = list(map(int, input().split()))
x = [-1] * (n + 1)
for _ in range(m):
    a, b = list(map(int, input().split()))
    x[b] = max(x[b], a)
ans = 0
bmx = 0
for i in range(n + 1):
    if bmx <= x[i]:
        ans += 1
        bmx = i
print(ans)

-events = [] -for i in range(m): +x = [-1] * (n + 1) +for _ in range(m): -    events.append((a, 0, i)) -    events.append((b - 1, 1, i)) -events.sort() -solved = [False] * m -todo = [] +    x[b] = max(x[b], a) -for i, etype, req in events: -    if etype == 0: -        todo.append(req) -    else: -        if not solved[req]: -            ans += 1 -            for req_id in todo: -                solved[req_id] = True -            todo = [] +bmx = 0 +for i in range(n + 1): +    if bmx <= x[i]: +        ans += 1 +        bmx = i