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

ab = []

for i in range(m):

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

    ab.append((a - 1, 2, i))

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



ab.sort()

ans = 0

separated = [False] * m

buf = set()

for island, ab, i in ab:

    if ab == 1:

        if separated[i]:

            continue

        else:

            ans += 1

            for j in buf:

                separated[j] = True

            buf.clear()

    else:

        buf.add(i)

print(ans)

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

disputes = []

for i in range(m):

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

    disputes.append((b, a))

disputes.sort()

ans = 0

most_right_bridge = 0

for b, a in disputes:

    if most_right_bridge <= a:

        ans += 1

        most_right_bridge = b

print(ans)

n, m = list(map(int, input().split()))
ab = []
for i in range(m):
    a, b = list(map(int, input().split()))
    ab.append((a - 1, 2, i))
    ab.append((b - 1, 1, i))
ab.sort()
ans = 0
separated = [False] * m
buf = set()
for island, ab, i in ab:
    if ab == 1:
        if separated[i]:
            continue
        else:
            ans += 1
            for j in buf:
                separated[j] = True
            buf.clear()
    else:
        buf.add(i)
print(ans)

n, m = list(map(int, input().split()))
disputes = []
for i in range(m):
    a, b = list(map(int, input().split()))
    disputes.append((b, a))
disputes.sort()
ans = 0
most_right_bridge = 0
for b, a in disputes:
    if most_right_bridge <= a:
        ans += 1
        most_right_bridge = b
print(ans)

-ab = [] +disputes = [] -    ab.append((a - 1, 2, i)) -    ab.append((b - 1, 1, i)) -ab.sort() +    disputes.append((b, a)) +disputes.sort() -separated = [False] * m -buf = set() -for island, ab, i in ab: -    if ab == 1: -        if separated[i]: -            continue -        else: -            ans += 1 -            for j in buf: -                separated[j] = True -            buf.clear() -    else: -        buf.add(i) +most_right_bridge = 0 +for b, a in disputes: +    if most_right_bridge <= a: +        ans += 1 +        most_right_bridge = b