#include <iostream>
#include <cstdio>
#include <cstring>
int n,m,ans,p[1010];
void Make(int *a){for(int i=1;i<=n;i++)p[i] = i;}
int Find(int x){return x==p[x]?x:(p[x]=Find(p[x]));}
void Union(int x,int y){p[Find(x)]=Find(y); }
int main(){
while (scanf("%d %d", &n, &m) != EOF && n){
Make(p);
while(m--){
int x,y;
scanf("%d%d",&x,&y);
Union(x,y);
}
ans=0;
for(int i=1;i<=n;i++)
if(Find(i)==i)ans++;
printf("%d\n",--ans);
}
return 0;
}