N, M = gets.split.map(&:to_i)
A = [*0..N]
$<.each{|ln|
	x, y, = ln.split.map(&:to_i)
	a, b = [x, y].map{|i| i = A[i] until i == A[i]; i }
	A[x] = A[y] = A[a] = A[b] = [a, b].min
}
p A.each_with_index.count{|a,i| a == i }-1
