#include <bits/stdc++.h>
using std::cerr;
using std::cin;
using std::max;
using std::min;
int n, m, K, f[200005 << 1], a, b, cnt;
int find(int x) { return f[x] ^ x ? f[x] = find(f[x]) : x; }
int merge(int x, int y) {
  int f1 = find(x), f2 = find(y);
  if (f1 ^ f2) f[f1] = f2;
}
int main() {
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 1; i <= n + m; i++) f[i] = i;
  for (int i = 1; i <= K; i++) scanf("%d%d", &a, &b), merge(a, b + n);
  for (int i = 1; i <= n + m; i++)
    if (find(i) == i) cnt++;
  printf("%d\n", cnt - 1);
  return 0;
}
