class UnionFind
	Node = Struct.new(:p, :h)
	def initialize(size); @p, @r = size.times.to_a, [0]*size; end
	def unite(i, j); k, l = parent(i), parent(j); if @r[k] < @r[l]; @p[k] = l; @r[l] += 1 if @r[k] == @r[l]; else; @p[l] = k; end; end
	def united?(i, j); parent(i) == parent(j); end
	def parent(i); j = i; until i == @p[i]; j, i = i, @p[j] = @p[i]; end; i; end
end

n, m = gets.split.map &:to_i
uf = UnionFind.new n
m.times do
  a, b = gets.split.map &:to_i
  uf.unite(a-1, b-1)
end

require "set"
s = Set.new
n.times do |i|
  s << uf.parent(i)
end

p s.size-1