(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 groups = new Array(n).fill(0).map((v, i) => [i]);
    for (const road of roads) {
        let from = road[0];
        let to = road[1];
        const fromGroup = groups.findIndex(group => group.indexOf(from) !== -1);
        const toGroup = groups.findIndex(group => group.indexOf(to) !== -1)
        if(fromGroup == toGroup){
            continue;
        }
        groups[fromGroup] = groups[fromGroup].concat(groups[toGroup]);
        groups.splice(toGroup, 1);
    }

    console.log(groups.length - 1);
})()
