class UnionFind: def __init__(self, N): self.par = [-1] * N self.size = [1] * N def root(self, x): while self.par[x] >= 0: x = self.par[x] return x def union(self, x, y): rx = self.root(x) ry = self.root(y) if rx == ry: return if self.size[rx] < self.size[ry]: rx, ry = ry, rx self.par[ry] = rx self.size[rx] += self.size[ry] def find(self, x, y): return self.root(x) == self.root(y) N, M = map(int, input().split()) box = UnionFind(M + 1) bi = {} info = [list(map(int, input().split())) for i in range(M)] for i in range(M): b, c = info[i] if bi.get(c, 0) == 0: bi[c] = b else: box.union(b, bi[c]) ans = 0 done = [False] * (M + 1) for i in range(1, M + 1): p = box.root(i) if not done[p]: ans += box.size[p] - 1 done[p] = True print(ans)