import sys input = sys.stdin.readline def main(): N, M = map(int, input().split()) l = [list(map(int, input().split())) for l in range(M)] class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) lout = [0] * N lin = [0] * N lans = [0] * N uf = UnionFind(N) for i in range(M): uf.union(l[i][0] - 1, l[i][1] - 1) lout[l[i][0] - 1] += 1 lin[l[i][1] - 1] += 1 ecpt = 0 for i in range(N): lans[i] = lout[i] - lin[i] if lout[i] == lin[i] == 0: ecpt += 1 rc = uf.group_count() if set(lans) == {0}: print(rc - 1 - ecpt) else: print(sum([i for i in lans if i > 0]) - 1 + (rc - 1) - ecpt) if __name__ == '__main__': main()