def root(x): p=x l=[p] while r[p]!=p: p=r[p] l+=[p] for p in l: r[p]=l[-1] return r[x] def union(x,y): rx=root(x) ry=root(y) if rx==ry: return if rx>ry: rx,ry=ry,rx r[ry]=rx v[rx]+=v[ry] v[ry]=[] return n,m=map(int,input().split()) r=list(range(n)) v=[[i] for i in range(n)] d=[0]*n for i in range(m): a,b=map(int,input().split()) a-=1 b-=1 d[a]+=1 d[b]-=1 union(a,b) c1=0 c2=0 for l in v: if len(l)<=1: continue c2+=1 c=sum(abs(d[i]) for i in l) c//=2 if c<=1: continue c1+=c-1 print(c1+c2-1)