Problem:
Score : 300 points

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M.
Road i connects City A_i and City B_i.

Snuke can perform the following operation zero or more times:

Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 100,000

1 \leq A_i < B_i \leq N

No two roads connect the same pair of cities.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

Print the answer.

Sample Input 1

3 1
1 2

Sample Output 1

1

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3.
After that,

We can travel between 1 and 2 directly.

We can travel between 1 and 3 directly.

We can travel between 2 and 3 by following both roads (2 - 1 - 3).

Solution in C# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;

namespace atc
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = new Solve();
            s.Exe();
            Console.ReadLine();
        }
    }
    
    class Solve
    {
        public void Exe()
        {
            var input = Console.ReadLine().Split().Select(int.Parse).ToList();
            var N = input[0]; var M = input[1];
            UnionFind uf = new UnionFind(N);
            for(int i = 0;i < M; i++)
            {
                var ab = Console.ReadLine().Split().Select(int.Parse).ToList();
                uf.unite(ab[0] - 1, ab[1] - 1);
            }
            int ans = 0;
            for(int i = 1; i < N; i++)
            {
                if(!uf.is_same(0, i))
                {
                    uf.unite(0, i);
                    ans++;
                }
            }
            Console.WriteLine(ans);
        }

    }

    class UnionFind
    {
        int size;
        long[] root;
        public UnionFind(int n)
        {
            this.size = n;
            root = new long[n];
            for(int i = 0;i < n; i++)
            {
                root[i] = -1;
            }
        }

        long find_root(long a)
        {
            if (root[a] < 0) return a;
            return find_root(root[a]);
        }

        public void unite(long a, long b)
        {
            if(a > b)
            {
                var t = a;
                a = b;
                b = t;
            }

            var a_root = find_root(a);
            var b_root = find_root(b);

            if(a_root != b_root)
            {
                root[a_root] += root[b_root];
                root[b_root] = a_root;
            }


        }

        public bool is_same(long a, long b)
        {
            var a_root = find_root(a);
            var b_root = find_root(b);
            return a_root == b_root;
        }


    }
}