N, M = readline.split(' ').map(&:to_i)
src = Array.new(N+1) {|idx| [idx]}
M.times do
  x,y, = readline.split(/\s/).map(&:to_i)
  src_x = src[x]
  src_y = src[y]
  next if src_x.equal?(src_y)
  src_x.concat(src_y)
  src_y.each do |idx|
    src[idx] = src_x
  end
end

puts(src.uniq.size - 1)
