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 n, m = map(int,input().split()) uf = UnionFind(n) din = [0] * n dout = [0] * n for i in range(m): u, v = map(int,input().split()) u -= 1 v -= 1 din[v] += 1 dout[u] += 1 uf.union(u, v) tmp = [] r = set() for i in range(n): tmp.append(dout[i] - din[i]) if din[i] > 0: r.add(uf.find(i)) ren = len(r) tmp.sort() # euler graph ans = 0 for i in range(n): if tmp[i] > 0: ans += tmp[i] #ans = min(ans, tar) if ans == 0: print(ren - 1) exit() # jun euler graph ans -= ren ans = max(ans, ren - 1) print(ans)