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

g = [[n] for i in range(n)]

for i in range(m):

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

  g[a-1].append(b-1)

k = 0

ans=0

while k <=n-1:

  e = n

  for i in g[k:]:

    e = min(e,min(i))

  if e==n:

    break

  ans+=1

  k = e

print(ans)
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)
n, m = list(map(int, input().split()))
g = [[n] for i in range(n)]
for i in range(m):
    a, b = list(map(int, input().split()))
    g[a - 1].append(b - 1)
k = 0
ans = 0
while k <= n - 1:
    e = n
    for i in g[k:]:
        e = min(e, min(i))
    if e == n:
        break
    ans += 1
    k = e
print(ans)

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)

-g = [[n] for i in range(n)] +t = [[n] for i in range(n)] -    g[a - 1].append(b - 1) +    t[a - 1].append(b - 1) +g = [] +for i in t: +    g.append(sorted(i)) -        e = min(e, min(i)) +        e = min(e, i[0])