n,m=map(int,input().split()) es=[] e=[[] for i in range(n)] for i in range(m): a,b=map(int,input().split()) a-=1 b-=1 es+=[(a,b)] e[a]+=[(b,1)] e[b]+=[(a,0)] class SCC(): def __init__(self,n): self.n=n self.e=[[] for i in range(self.n)] self.re=[[] for i in range(self.n)] return def add_edge(self,s,t): self.e[s]+=[t] self.re[t]+=[s] return def scc(self): v=[0]*self.n g=[0]*self.n o=[] for i in range(self.n): if v[i]==0: q=[i] while len(q)>0: s=q[-1] v[s]=1 while g[s]1 else 1 c1=0 c2=0 v=[0]*n for p in range(n): if v[p]==0: o=[] v[p]=1 q=[p] for s in q: o+=[s] for t,_ in e[s]: if v[t]==0: v[t]=1 q+=[t] d={u:i for i,u in enumerate(o)} es2=[] for i in o: for j,f in e[i]: if f: es2+=[(d[i],d[j])] c1+=1 c2+=solve(len(o),es2) print(c2-(c1==1))