#include<bits/stdc++.h>
using namespace std;
const int N=1010,M=2e4+10;
int p[N];
int find(int x)
{
if(x!=p[x])return p[x]=find(p[x]);
return p[x];
}
int main()
{
int n,m;
cin>>n>>m;
int res=n-1;
for(int i=1;i<=n;i++)p[i]=i;
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
a=find(a),b=find(b);
if(a!=b)p[b]=a,res--;
}
cout<<res<<endl;
}