#include<bits/stdc++.h>
#include <iostream>
#include<map>
#include <algorithm>
using namespace std;
typedef long long      ll;
#define mp             make_pair
#define pb             push_back
#define fi             first
#define se             second
#define sz(x)          (int)((x).size())
#define cena           ios_base::sync_with_stdio(false);cin.tie(NULL);
int t=1000001;
#define MOD  1000000007
class myData
{
    public:
    int t;
    int x;
    int y;
    int d;
    myData(int t,int x,int y,int d)
    : t(t),x(x),y(y),d(d)
    {
    }
    bool operator<(const myData& a) const
    {
        return (this->t < a.t);
    }
};
ll exponentMod(ll A, ll B)
{
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
    ll y;
    if (B % 2 == 0) {
        y = exponentMod(A, B / 2);
        y = (y * y) ;
    }
    else {
        y = A ;
        y = (y * exponentMod(A, B - 1) );
    }
    return (ll)((y ));
}
pair<int,int>  factorize(ll n) {
    int ans=1;
    int c=0;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            if(i!=2)
                ans*=i,c++;
            n/=i;
        }
    }
    if (n != 1) {
            if(n!=2)
        ans*=n,c++;
    }
    return {ans,c};
}
////////////    FENWICK TREE    //////////////////
map<ll,ll> bit;
int N1= 2e5;
void update(ll i){
    for(;i<=N1;i+=i&(-i))
        bit[i]++;
}
ll get(ll i){
    ll ans = 0;
    for(;i>0;i-=i&(-i))
        ans+=bit[i];
    return ans;
}
int now;
bool sortByVal(const myData &a,
               const myData &b)
{
    return (a.t<b.t);
}/*char s[1005][1005];
int vis[1005][1005];
//int a[15],b[15],c[15];
int n,m,p;
int check(int x,int y){
	if(x<1||x>n||y<1||y>m||s[x][y]=='#'||vis[x][y])return 0;
	return 1;
}struct node{
	int x,y;
};
int dx[10]={0,0,1,-1};
int dy[10]={1,-1,0,0};
queue<node>q[15];
void bfs(int k){
	node no,ne;
	int h=q[k].size();
	while(h--){
		no=q[k].front();
		q[k].pop();
		for(int i=0;i<4;i++){
			ne.x=no.x+dx[i];
			ne.y=no.y+dy[i];
			if(check(ne.x,ne.y)){
				vis[ne.x][ne.y]=1;
				q[k].push(ne);
				b[k]++;
			}
		}
	}
}*/

ll ans1=0;
/*ap<int,int>m;
int dfs(int i,int n) {
    m[i]=1;
    int j=2;
    while(abs(i*j)<=n){
            ans1+=j;
            if(m[i*j]==0)
            dfs(i*j,n);
            ans1+=j;
            if(m[-i*j]==0)
            dfs(-i*j,n);
            j++;
    }
}*/
#define MAXN   100003
int spf[MAXN];
ll  calc[MAXN+1];
/*ll nCk(ll n,ll k,ll mod) {
    return ((((calc[n]%mod)*exponentMod(calc[k],mod-2,mod))%mod)*exponentMod(calc[n-k],mod-2,mod))%mod;
}*/
void sieve()
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
set<int>getFactorization(int x)
{
    set<int> ret;
    while (x != 1)
    {
        ret.insert(spf[x]);
        x = x / spf[x];
    }
    return ret;
}
bool prime[MAXN+1];
void sieve2()
{
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=MAXN; p++)
    {
        if (prime[p]==true)
        {
            for (int i=p*p; i<=MAXN; i += p)
                prime[i] = false;
        }
    }
}
int MEX(int a[],int n) {
    set<int>s;
    for(int i=0;i<n;i++) {
        s.insert(a[i]);
    }
    int c=0;
    for(auto i=s.begin();i!=s.end();i++) {
       if(*i==c) {
        c++;
       }
       else
        break;
    }
    return c;
}
int smallestDivisor(int n)
{
    if (n % 2 == 0)
        return 2;
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return i;
    }
    return n;
}
int setBitNumber(int n)
{
    if (n == 0)
        return 0;

    int msb = 0;
    n = n / 2;
    while (n != 0) {
        n = n / 2;
        msb++;
    }
    return msb;
}
void solve() {

}
vector<int>v[100002];
void dfs(int visited[],int u) {
    visited[u]=1;
    for(int i=0;i<v[u].size();i++) {
        if(!visited[v[u][i]])
            dfs(visited,v[u][i]);
    }
}
int main()
{
    cena
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++) {
        int x,y;
        cin>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
    }
    int c=0;
    int visited[n+1];
    memset(visited,0,sizeof(visited));
    for(int i=1;i<=n;i++) {
        if(!visited[i]) {
            c++;
            dfs(visited,i);
        }
    }
    cout<<c-1<<endl;
	return 0;
}









Measured memory_bytes: 10016