#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N = 1000;
int father[N],rank[N],ans[N];
void make_set(int n){
for(int i=1;i<=n;i++){
father[i] = i;
rank[i] = 0;
}
}
int find(int x){
return x==father[x]?x:find(father[x]);
}
void Union(int x,int y){
x = find(x);y = find(y);
if(x==y)return;
if(rank[x]<rank[y])father[x]=y;
else{
if(rank[x]==rank[y])rank[x]++;
father[y] = x;
}
}
int main()
{
int n, m, i, sum,a ,b;
while(scanf("%d",&n),n){
sum = 0;
scanf("%d",&m);
memset(ans,0,sizeof(ans));
make_set(n);
for(i=1;i<=m;i++){
scanf("%d%d",&a,&b);
Union(a,b);
}
for(i=1;i<=n;i++){
if(i==father[i])sum++;
}
printf("%d\n",sum-1);
}
return 0;
}