<pre name="code" class="cpp">#include <bits/stdc++.h>
using namespace std;
int M, N, *pre, a, b, cnt;
void init()
{
pre = new int[M+5];
for (int i = 1; i <= M; i++)
pre[i] = i;
}
int find(int x)
{
if (x != pre[x]) pre[x] = find(pre[x]);
return pre[x];
}
void Iunion(int x, int y)
{
x = find(x);
y = find(y);
if (x != y) pre[x] = y, --cnt;
}
int main(int argc, char const *argv[])
{
while (cin >> M && M)
{
cin >> N;
cnt = M - 1;
init();
while (N--)
{
cin >> a >> b;
Iunion(a, b);
}
delete[] pre;
cout << cnt << endl;
}
return 0;
}