n, m = gets.chomp.split(" ").map(&:to_i)
hash = Hash.new(0)
(1..n).each do |i|
    hash[i.to_s] -= 1
end
p hash
m.times do |i|
    a, b = gets.chomp.split(" ")
    #.map(&:to_i)
    if hash[a] == -1 && hash[b] == -1
        hash[b] = a
        hash[a] = a
    elsif hash[a] != -1 && hash[b] == -1
         hash[b] = a
    elsif hash[a] == -1 && hash[b] != -1
        if hash[b] <= a
            hash[a] = a
            
            hash.each do |i,j|
                if j == hash[b]
                    hash[i] = a
                end
            end
        else
            hash[a] = hash[b]
        end
    elsif hash[a] != -1 && hash[b] != -1
        if hash[a] >= hash[b]
            hash.each do |i,j|
                if j == hash[b]
                    hash[i] = hash[a]
                end
            end
        else
            hash.each do |i,j|
                if j == hash[a]
                    hash[i] = hash[b]
                end
            end
        end
        
    end
end
count = 0
arr = []
hash.each do |i,j|
  if j == -1
arr << j
  else
    count += 1
  end
end
puts arr.size - 1 + count