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++ (Clang 10.0.0) (judge verdict: Accepted)

//
//  main.cpp
//  Atcoder1
//
//  Created by Hamske on 2020/09/17.
//  Copyright © 2020 Author. All rights reserved.
//

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

//#define pb push_back
//#define mp make_pair
#define fi first
#define se second

typedef long long ll;

//#define rep(_i,iv,n) for(int _i=(int)(iv);_i<(int)(n);++_i)
#define rep0(_i,n) for(int _i=0;_i<(int)(n);++_i)
//#define REP(_i,iv,_l,n) for(int _i=(int)(iv),_l=(int)(n);_i<_l;++_i)
#define REP0(_i,_l,n) for(int _i=0,_l=(int)(n);_i<_l;++_i)

//#define all(x) (x).begin(),(x).end()
#define REPV(v,_itr,_end) for(auto _itr=(v).begin(),_end=(v).end();_itr!=_end;++_itr)
#define REPVC(v,_itr,_end) for(auto _itr=(v).cbegin(),_end=(v).cend();_itr!=_end;++_itr)

#define td typedef
//#define tds(stl,type,name) typedef stl<type > name;
#define tdv(type,name) typedef vector<type > name;

typedef vector<bool> vb;
typedef vector<vb > vvb;
typedef vector<int> vi;
typedef vector<vi > vvi;
typedef vector<ll> vll;
typedef vector<vll > vvll;

#define INF 1<<30

td pair<int, int > pii;
td priority_queue<pii,vector<pii>,greater<pii> > pq;

int find(vi& G,int i){
    int pi=i;
    while (pi!=G[pi]) {
        pi=G[pi];
    }
    while (G[i]!=pi) {
        int temp=G[i];
        G[i]=pi;
        i=temp;
    }
    return pi;
}
bool sameRoot(vi& G,int i,const int j,int *const pi,int *const pj){
    if(i==j){
        return true;
    }
    int pi1=i;
    bool sameFlg=false;
    while (pi1!=G[pi1]) {
        pi1=G[pi1];
        if(pi1==j){
            sameFlg=true;
            break;
        }
    }
    while (G[i]!=pi1) {
        int temp=G[i];
        G[i]=pi1;
        i=temp;
    }
    if(sameFlg){
        return true;
    }
    *pi=pi1;
    *pj=find(G,j);
    return pi1==*pj;
}


void mergeUF(vi& G,vi& rank,const int p1,const int p2){
    if(rank[p1]>rank[p2]){
        G[p2]=p1;
    }else if(rank[p1]>rank[p2]){
        G[p1]=p2;
    }else{
        G[p2]=p1;
        ++rank[p1];
    }
}

int main(int argc, const char * argv[]) {
    int N,M;
    cin>>N>>M;
    
    vi G(N),rank(N,0);
    rep0(i, N){
        G[i]=i;
    }
    int ufNum=N;
    
    rep0(i, M){
        int a1,b1;
        cin>>a1>>b1;
        --a1;
        --b1;
        int pa,pb;
        if(!sameRoot(G, a1, b1, &pa, &pb)){
            mergeUF(G, rank, pa, pb);
            --ufNum;
        }
    }
    cout<<(ufNum-1)<<endl;
    return 0;
}