require "set"
n, m = gets.split.map(&:to_i)

hash = {}
(1..n).each {|i| hash[i] = [] }
m.times do
  a, b = gets.split.map(&:to_i)
  hash[a] << b
  hash[b] << [a]
end

set = Set.new
count = 0
hash.keys.each do |num|
  next if set.include?(num)

  arr = hash[num] || []
  count += 1
  queue = [] + arr
  while !queue[0].nil?
    nn = queue.shift
    set << nn
    hash[nn]&.each do |mm|
      queue << mm if !set.include?(mm)
    end
  end
end
puts count - 1
