n,m=gets.split.map(&:to_i) lc=[] n.times do lc << [] end n.times do b,c=gets.split.map(&:to_i).map{|x|x-1} lc[c] << b end l=[] m.times do |i| l << i end lc.each do |lcc| a=0 lcc.uniq.each.with_index do |ll,i| if i==0 a=ll next end b=ll s=[a,b] k=a while l[k]!=k k=l[k] s << k end k=b while l[k]!=k k=l[k] s << k end min=s.min s.uniq do |t| l[t]=min if l[t]!=min end end end c=[0]*n m.times do |i| if l[i]!=i&&l[i]!=l[l[i]] l[i]=l[l[i]] end c[l[i]]+=1 end ans=m m.times do |i| if l[i]==i ans-=1 end end puts ans