(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 cities = new Array(n).fill(0).map((v, i) => i);
    for (const road of roads) {
        let from = road[0];
        let to = road[1];
        console.log(from, to);
        if(cities[from] == cities[to]){
            continue;
        }
        let group1 = cities[from];
        let group2 = cities[to];
        if(cities[from] > cities[to]){
            group1 = cities[to];
            group2 = cities[from];
        }

        cities = cities.map(v => {
            if(v === group2){
                console.log(v);
                return group1;
            }
            return v;
        })
    }

    let citieGroups = new Set();
    for (const group of cities) {
        citieGroups.add(group);
    }
    console.log(citieGroups.size - 1);
})()
