#include<iostream>
using namespace std;
int pre[110],rank[110],n;
int find(int x){
int r=x;
while(pre[r]!=-1)
r=pre[r];
while(x!=r){
int q=pre[x];
pre[x]=r;
x=q;
}
return r;
}
void unionone(int a,int b){
int t1=find(a);
int t2=find(b);
if(rank[t1]>rank[t2])
pre[t2]=t1;
else
pre[t1]=t2;
if(rank[t1]==rank[t2])
rank[t2]++;
n--;
}
int main(){
int m,i,begin,end;
while(1){
scanf("%d""%d",&n,&m);
if(n==0&&m==0)
break;
for(i=0;i<=n;i++){
rank[i]=0;
pre[i]=-1;
}
for(i=0;i<m;i++){
scanf("%d""%d",&begin,&end);
if(find(begin)!=find(end))
unionone(begin,end);
}
printf("%d\n",n-1);
}
return 0;
}