n,m = gets.split.map &:to_i

arr = Array.new(n + 1).map{ Array.new }

m.times{
	a,b = gets.split.map &:to_i
	arr[a] << b
	arr[b] << a
}

count = 0

(1..n).each{ |x|
	if arr[x] == nil then
		next
	end

	temp = arr[x]
	
	arr[x] = nil

	while temp.size > 0
		n_temp = {}
		
		temp.each{ |t|
			if arr[t] != nil then
				arr[t].each{ |i|
					n_temp[i] = true
				}
				arr[t] = nil
			end
		}
		
		temp = n_temp.keys
	end
	
	count += 1
}

puts count - 1