#include <cstdio>

int father[1010];
int n,m;
void init(int val){
	for(int i=1;i<=val;i++){
		father[i]=i;
	}
}
int find(int val){
	if(father[val]==val) return val;
	return find(father[val]);
}
void combine(int x,int y){
	int tx = find(x);
	int ty = find(y);
	if(tx!=ty)
		father[tx]=ty;
}
int main(){
	while(scanf("%d",&n)!=EOF&&n){
		scanf("%d",&m);
		init(n);
		int a,b;
		for(int i=0;i<m;i++){
			scanf("%d%d",&a,&b);
			combine(a,b);
		}
		int ans = 0;
		for(int i=1;i<=n;i++){
			if(father[i]==i)
				ans++;
		}
		printf("%d\n",ans-1);
	}
	return 0;
}
