#include "iostream"
#include "math.h"
#include "string.h"
#include "algorithm"
using namespace std;
int f [10005];
void init()
{
for(int i = 0 ; i < 10005 ; i++)
f[i] = i;
}
//非路径压缩
int find(int x)
{
while(x != f[x]) x = f[x];
return x;
}
//路径压缩 将所有结点的上级都更新成祖宗
int find2(int x)
{
if(f[x] == x) return x;
else
{
f[x] = find2(f[x]);
return f[x];
}
}
void marge(int a , int b)
{
int fa = find(a);
int fb = find(b);
if(fa != fb)
f[fb] = fa;
}
int main()
{
//n条路,m条指令
int n , m;
int a , b;
while(scanf("%d",&n))
{
if(n == 0) break;
init();
//让所有路都连起来需要再建多少条?
scanf("%d",&m);
while( m-- )
{
scanf("%d%d",&a,&b);
marge(a,b);
}
//扫描共有多少联通的路 需要建的路就是该数目减去一
int cnt = 0;
for(int i = 1 ; i <= n ; i++)
if(find2(i) == i) cnt ++;
cout << cnt - 1 <<endl;
}
return 0;
}