#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2e5 + 3;
vector<int> c[N];
vector<int> e[N];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, q;
  cin >> n >> m >> q;
  for (int it = 0; it < q; ++it) {
    int r, C;
    cin >> r >> C;
    c[r].push_back(C);
  }
  for (int i = 1; i <= n; ++i) {
    int esz = c[i].size();
    for (int j = 1; j < esz; ++j) {
      int prev = c[i][j - 1], now = c[i][j];
      e[prev].push_back(now);
      e[now].push_back(prev);
    }
  }
  vector<int> vis(m + 1, 0);
  int cc = 0;
  for (int i = 1; i <= m; ++i) {
    if (vis[i] == 0) {
      ++cc;
      function<void(int)> dfs = [&](int i) {
        assert(i >= 1 && i <= m);
        if (vis[i] != 0) {
          assert(vis[i] == cc);
          return;
        }
        vis[i] = cc;
        for (int v : e[i]) {
          dfs(v);
        }
      };
      dfs(i);
    }
  }
  int cnt_empty_r = 0;
  for (int i = 1; i <= n; ++i) {
    if (c[i].empty()) {
      ++cnt_empty_r;
    }
  }
  cout << cnt_empty_r + cc - 1 << '\n';
}
