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

const int N=100010;
int n,m,cnt,father[N];

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

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