/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n, m, cnt;
int f[1005];

int Find(int x){
	return x == f[x] ? x : Find(f[x]);
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	while(scanf("%d", &n) == 1){
		if(!n) break;
		scanf("%d", &m);
		for (int i = 1; i <= n; i++){
			f[i] = i;
		}
		for (int i = 0; i < m; i++){
			int u, v;
			scanf("%d %d", &u, &v);
			int x = Find(u), y = Find(v);
			if(x != y){
				f[x] = y;
			}
		}
		set<int>st;
		for (int i = 1; i <= n; i++){
			f[i] = Find(i);
			st.insert(f[i]);
		}
		printf("%d\n", st.size() - 1);
	}

	return 0;
}
/**/