#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
void bfs(vector<vector<long long int> > in, vector<bool> &visited,
         long long int v, long long int sv) {
  visited[sv] = true;
  queue<long long int> q;
  q.push(sv);
  vector<long long int> ans;
  while (!q.empty()) {
    long long int vertex = q.front();
    q.pop();
    for (auto i : in[vertex]) {
      if (!visited[i]) {
        q.push(i);
        visited[i] = true;
      }
    }
  }
}
void Panda() {
  long long int v, e;
  cin >> v >> e;
  vector<vector<long long int> > in(v);
  for (long long int i = 0, x, y; i < e; i++) {
    cin >> x >> y;
    in[x].emplace_back(y), in[y].emplace_back(x);
  }
  vector<bool> visited(v);
  long long int cnt = 0;
  for (long long int i = 0; i < v; i++)
    if (!visited[i]) {
      bfs(in, visited, v, i);
      cnt++;
    }
  cout << cnt - 1 << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  Panda();
  return 0;
}
