#include <bits/stdc++.h>
using namespace std;

int fa[1005], he[1005];
int find(int n) {
    if (n == fa[n]) return n;
    fa[n] = find(fa[n]);
    return fa[n];
}


int main() {
    int N, M;
    while (cin >> N >> M && N != 0) {
        for (int i = 1; i <= N; i++) {
            fa[i] = i;
        };
        // 已经建设的道路数量
        int sum = 0;
        for (int i = 0; i < M; i++) {
            int x, y;
            cin >> x >> y;
            int fx = find(x);
            int fy = find(y);
            if (fx != fy) {
                fa[fx] = fy;
                sum++;
            }
        }
        cout <<N - 1-sum << endl;

    }
}