include<bits/stdc++.h>

using namespace std;
int f[100010];
void into(int x)
{
for(int i=0;i<x;i++)
f[i]=i;
}
int find(int x)
{
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
void join(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
f[x]=y;
}
int main()
{
set<int> s;
int n,m,a,b;
cin>>n>>m;
into(n);
while(m--)
{
cin>>a>>b;
join(a,b);
}
for(int i=0;i<n;i++)
s.insert(find(f[i]));
cout<<s.size()-1<<endl;
return 0;
}</int>