N,M=map(int,input().split()) par=[i for i in range(N)] rank=[0]*(N) friend=[0]*N block=[0]*N size=[1]*N def find(x): if par[x]==x: return x else: par[x]=find(par[x]) return par[x] #同じ集合か判定 def same(x,y): return find(x)==find(y) def union(x,y): x=find(x) y=find(y) if x==y: return if rank[x]>rank[y]: par[y]=x size[x]+=size[y] else: par[x]=y size[y]+=size[x] if rank[x]==rank[y]: rank[y]+=1 D={} for i in range(N): a,b=map(int,input().split()) a-=1 b-=1 if b not in D: D[b]=[a] else: D[b].append(a) for i in D: d=D[i] for j in range(len(d)-1): union(d[j],d[j+1]) E=[0]*N ans=0 for i in range(N): d=find(i) if E[d]==0: E[d]=1 else: ans+=1 print(ans)