#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <numeric>
#include <array>
using namespace std;
int INF = 1e9 + 7;
int mod = 998244353;
long double PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998626034825342117;
struct Edge {
    long long to;
    long long cost;
};
void dijkstra(const vector<vector<Edge> >& G, long long s, vector<long long>& dis) {//Gは隣接グラフ,sは始点,距離の結果をdisに格納
    long long N = G.size();
    dis.resize(N, INF);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq;  // 「仮の最短距離, 頂点」が小さい順に並ぶ
    dis[s] = 0;
    pq.emplace(dis[s], s);
    while (!pq.empty()) {
        pair<int, int> p = pq.top();
        pq.pop();
        int v = p.second;
        if (dis[v] < p.first) {
            continue;
        }
        for (auto& e : G[v]) {
            if (dis[e.to] > dis[v] + e.cost) {
                dis[e.to] = dis[v] + e.cost;
                pq.emplace(dis[e.to], e.to);
            }
        }
    }
    return;
}
long double kodohou(long double N) {
    return N * 180.0 / PI;
}
struct mint {
    int x;
    mint(int x = 0) :x((x% mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }
    mint pow(int t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
struct UnionFind {
    vector<int> par, size;
    UnionFind(int x) {
        par.resize(x);
        size.resize(x, 1);
        for (int i = 0; i < x; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if (par[x] == x)
            return x;
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return;
        if (size[x] < size[y]) {
            par[x] = y;
            size[y] += size[x];
        }
        else {
            par[y] = x;
            size[x] += size[y];
        }
    }
};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false;
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            return false;
        }
    }
    return true;
}
signed gcd(long long x, long long y) {
    if (y == 0)return x;
    return gcd(y, x % y);
}
signed lcm(long long x, long long y) {
    return x / gcd(x, y) * y;
}
long long RepeatSquaring(long long N, long long P, long long M) {
    if (P == 0) return 1;
    if (P % 2 == 0) {
        long long t = RepeatSquaring(N, P / 2, M);
        return t * t % M;
    }
    return N * RepeatSquaring(N, P - 1, M);
}
int dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 };
long long Fibonacci(long long A) {
    if (A == 1)return 1;
    vector<long long>B(A + 1); B[0] = 1; B[1] = 1;
    for (int i = 2; i <= A; i++) { B[i] = B[i - 1] + B[i - 2]; B[i] %= mod; }
    return B[A];
}
int mod_pow(int x, int y) {
    int res = 1;
    while (y > 0) {
        if (y % 2) {
            res = res * x % mod;
        }
        x = x * x % mod;
        y /= 2;
    }
    return res;
}
int fac[100005], finv[100005], inv[100005];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < 100005; i++) {
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}
int COM(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
vector<int> sieve(int max) {
    vector<bool>prime(max + 1, true);
    vector<int>A;
    for (int i = 2; i <= max; ++i)
        if (prime[i]) {
            A.push_back(i);
            for (int j = 2; i * j <= max; ++j)
                prime[i * j] = false;
        }
    return A;
}class smart_sieve {
    intmax_t L, R, M;
    vector<int> small;
    vector<vector<intmax_t>> large;
    vector<intmax_t> aux;
public:
    smart_sieve(intmax_t L, intmax_t R) : L(L), R(R), M(sqrt(R) + 1) {
        small.resize(M);
        iota(small.begin(), small.end(), 0);
        large.resize(R - L);
        aux.assign(R - L, 1);

        for (intmax_t i = 2; i * i < R; ++i) {
            if (small[i] < i) continue;
            small[i] = i;
            for (intmax_t j = i * i; j < M; j += i)
                if (small[j] == j) small[j] = i;

            for (intmax_t j = (L + i - 1) / i * i; j < R; j += i) {
                intmax_t k = j;
                do {
                    if (aux[j - L] * aux[j - L] > R) break;

                    large[j - L].push_back(i);
                    aux[j - L] *= i;
                    k /= i;
                } while (k % i == 0);
            }
        }
    }
};
int main() {
    int N, M; cin >> N >> M;
    vector<int>A(M);
    vector<int>B(M);
    UnionFind C(N);
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i];
        C.unite(A[i]-1, B[i]-1);
    }
    map<int,int>D;
    for (int i = 0; i < N; i++) {
        D[C.find(i)]++;
    }
    int ans = 0;
    for (int i = 0; i < N + 2; i++) {
        if (D[i] != 0)ans++;
    }
    cout << ans-1 << endl;
}

Measured memory_bytes: 9664