class UF: def __init__(self,n): self.p = [-1]*n def f(self,x): if self.p[x]<0: return x else: self.p[x] = self.f(self.p[x]); return self.p[x] def u(self,x,y): x = self.f(x); y = self.f(y) if x==y: return if -self.p[x]<-self.p[y]: x,y = y,x self.p[x] += self.p[y]; self.p[y] = x n,m = map(int,input().split()) a = [0]*n; b = [0]*n; c = []; uf = UF(n) for _ in range(m): u,v = map(int,input().split()) a[u-1] += 1; b[v-1] += 1; uf.u(u-1,v-1) ans = len(set(uf.f(i) for i in range(n)))-1 for u,v in zip(a,b): if u>v: c.append(u-v) if u==v==0: ans -= 1 print(ans+max(sum(c)-1,0))