#include <iostream>
using namespace std;

const int N = 100010;

int father[N];//祖宗节点
int height[N];//祖宗节点高度

void initial(int n) {
    for (int i = 1; i <= n; i++) {
        father[i] = i;
        height[i] = 0;
    }
}

int find(int x) { //找到祖宗节点
    if (father[x] != x) father[x] = find(father[x]);
    return father[x];
}


void Union(int a, int b) {
    a = find(a);
    b = find(b);
    if (a != b) {//两个节点不是一个集合
        if (height[a] < height[b]) father[a] = b;
        else if (height[a] > height[b]) father[b] = a;
        else {
            father[a] = b;
            height[b]++;
        }
    }
    return;
}


int main() {
    int n, m;
    while (cin >> n >> m) {
        if (n == 0) break; //输入结束

        initial(n);
        while (m--) {
            int a, b;
            cin >> a>>b;
            Union(a, b); //并
        }

        int ans = -1;
        for (int i = 1; i <= n; i++) {
            if (find(i) == i) ans++;
        }
        cout << ans << endl;
    }

}
// 64 位输出请用 printf("%lld")