https://vjudge.net/contest/280495#problem/A
#include<iostream>
#include<cstdio>
using namespace std;
int n,m,i,j,k,l,x1,y1,x,y,f[100000],ans;
int find(int x)
{
if (f[x]==x) return x;
return f[x]=find(f[x]);
}
int main()
{
while (scanf("%d",&n))
{
if (n==0) break;
scanf("%d",&m);
for (i=1; i<=n; i++)
f[i]=i;
for (i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
x1=find(x);
y1=find(y);
if (x1!=y1) f[x1]=y1;
}
ans=0;
for (i=1; i<=n; i++)
if (f[i]==i) ans++;
printf("%d\n",ans-1);
}
}