n,m = gets.strip.split.map(&:to_i)
t = 0
c = Array.new(n,0)
m.times do |a|
  a,b = gets.strip.split.map{|e| e.to_i - 1}
  if c[a] != 0
    c[b] = c[a]
  elsif c[b] != 0
    c[a] = c[b]
  else
    t += 1
    c[a] = c[b] = t
  end
end
p c.count(0) + t-1
