from sys import stdin input=lambda :stdin.readline()[:-1] n,m=map(int,input().split()) edge=[[] for i in range(n)] IN=[0]*n OUT=[0]*n for i in range(m): a,b=map(lambda x:int(x)-1,input().split()) IN[a]+=1 OUT[b]+=1 edge[a].append(b) edge[b].append(a) G=[] seen=[0]*n for i in range(n): if seen[i]: continue todo=[i] seen[i]=1 res=0 flag=False while todo: v=todo.pop() if IN[v]>OUT[v]: flag=True res+=IN[v]-OUT[v] for u in edge[v]: if seen[u]==0: seen[u]=1 todo.append(u) G.append([res,flag]) ans=0 cnt1=0 cnt2=0 for x,y in G: ans+=x if y: ans-=1 else: cnt2+=1 if cnt1: ans-=1 print(ans+max(0,cnt2-1))