class UnionFind{
    constructor(n){
        this.par = new Array(n);
        for(let i = 0;i < n;i++)this.par[i] = i;
    }

    root(x){
        if(x === this.par[x])return x;
        return this.par[x] = this.root(this.par[x]);
    }

    unite(x, y){
        const rx = this.root(x);
        const ry = this.root(y);
        if(rx === ry)return;
        this.par[rx] = ry;
    }

    isSame(x, y){
        return this.root(x) === this.root(y);
    }
}

(async () => {
    let input = '';
    for await (const chunk of process.stdin) input += chunk;
    const lines = input.split(/\n/);
    const [n, m] = lines[0].split(/\s/).map(v => +v);
    const roads = lines.slice(1, m + 1).map(line => line.split(/\s/).map(v => v - 1));

    let par = new UnionFind(n);
    for (const road of roads) {
        par.unite(road[0], road[1]);
    }

    console.log(par.par.filter((p, i) => p === i).length - 1);
})()
