#include<bits/stdc++.h>
#define f(s,f,p) for(int i=(s);i<=(f);i+=(p))
using namespace std;
map<int,int> f;
void make_family(int n){
f(1,n,1){
f[i]=i;
}
}
int parentof(int n){
if(n==f[n]){
return n;
}
return parentof(f[n]);
}
void join_family(int n,int m){
f[parentof(n)]=parentof(m);
}
int main(){
int n,m,t1,t2,ans;
f(1,2,-1){
ans=0;
cin>>n;
if(!n){
break;
}
make_family(n);
cin>>m;
f(1,m,1){
cin>>t1>>t2;
join_family(t1,t2);
}
f(1,n,1){
if(i==parentof(i)){
ans++;
}
}
cout<<ans-1<<endl;
}
return 0;
}