class DisjointSet
	def initialize(n)
		@parent = Array.new(n){-1}
	end
	def find(a)
		return a if @parent[a]<0
		@parent[a] = find(@parent[a])
	end
	def union(a, b)
		x = find(a)
		y = find(b)
		return x if x == y
		if size(x) < size(y)
			t = x
			x = y
			y = t
		end
		@parent[x] += @parent[y]
		@parent[y] = x
		x
	end
	def size(a)
		-@parent[find(a)]
	end
end

N,M=gets.split.map(&:to_i)
ds=DisjointSet.new(N)
M.times{
	a,b=gets.split.map(&:to_i)
	ds.union(a-1,b-1)
}
r=0
(1 .. N-1).each{|i|
	if ds.find(i)!=ds.find(0)
		ds.union(i,0)
		r+=1
	end
}
puts r
