#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 10;
int f[MAXN];
int find(int x) {
  if (f[x] == x) return x;
  return f[x] = find(f[x]);
}
void unionn(int u, int v) { f[find(u)] = find(v); }
int main() {
  std::ios::sync_with_stdio(false);
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; i++) f[i] = i;
  int cnt = 0;
  while (q--) {
    int u, v;
    cin >> u >> v;
    if (find(u) == find(n + v)) {
      continue;
    } else {
      cnt++;
      unionn(u, n + v);
    }
  }
  cout << n + m - 1 - cnt << '\n';
}
