#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct UnionFind {
  vector<int> par;
  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++) par[i] = i;
  }
  int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  UnionFind tree(n);
  for (int i = (0); i < (m); ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree.unite(a, b);
  }
  map<int, int> check;
  int ans = -1;
  for (int i = (0); i < (n); ++i) {
    int c = tree.root(i);
    if (check.find(c) == check.end()) {
      ans++;
      check[c] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
