#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  vector<vector<int>> e(n + m + 1);
  for (int i = 0; i < q; ++i) {
    int r, c;
    cin >> r >> c;
    e[r].push_back(c + n);
    e[c + n].push_back(r);
  }
  vector<bool> vis(n + m + 1);
  function<void(int)> dfs = [&](int f) {
    vis[f] = true;
    for (int i : e[f]) {
      if (!vis[i]) {
        dfs(i);
      }
    }
  };
  int ans = -1;
  for (int i = 1; i <= n + m; ++i) {
    if (!vis[i]) {
      dfs(i);
      ++ans;
    }
  }
  cout << ans << endl;
  return 0;
}
