N, M = gets.split.map(&:to_i)
Pairs = readlines.map(&:chomp!).map { |e| e.split.map(&:to_i) }
hash = {}
(1..N).to_a.each do |n|
  hash[n] = [n]
end

while !Pairs.empty?
  e = Pairs.shift
  hash[e.first] << e.last
  hash[e.last] << e.first
end

puts hash.values.group_by { |i| i.sort }.size - 1
