#include <bits/stdc++.h>
using namespace std;

int fa[100010];

int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

int main () {
	int n, m; scanf("%d%d", &n, &m);
	int tot = n - 1;
	for (int i = 1; i <= n; i++) {
		fa[i] = i;
	}
	for (int i = 1; i <= m; i++) {
		int x, y; scanf("%d%d", &x, &y);
		x = find(x), y = find(y);
		if (x == y) continue;
		tot--;
		fa[x] = y;
	}
	printf("%d\n", tot);
	return 0;
}
