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
    if c[b] == 0
      t += 1
      c[a] = c[b] = [a,b]
    else
      c[a] = c[b]
    end
  else
    if c[b] == 0
      c[b] = c[a]
    elsif c[a] != c[b]
      t -= 1
      c[a].concat(c[b])
      c[b] = c[a]
    end
  end
end
p c.count(0) + t-1
