from collections import deque

n, m = [int(x) for x in input().split()]
graf = []
for _ in range(n):
    graf.append([])
for _ in range(m):
    a, b = [int(x) for x in input().split()]
    graf[a - 1].append(b - 1)
wynik = -m
for i in range(n):
    stos = deque()
    widziano = n * [False]
    stos.append(i)
    widziano[i] = True
    while stos:
        v = stos.pop()
        for w in graf[v]:
            if not widziano[w]:
                widziano[w] = True
                stos.append(w)
                wynik += 1
print(wynik)
