#include <bits/stdc++.h>
const int maxn = 4e5 + 10;
int f[maxn];
bool g[maxn] = {0};
int fa(int x) {
  if (f[x] == x) return x;
  f[x] = fa(f[x]);
  return f[x];
}
int main() {
  int n, m, q, x, y;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = n + 1; i <= n + m; i++) f[i] = i;
  while (q--) {
    scanf("%d%d", &x, &y);
    y += n;
    int r1 = fa(x), r2 = fa(y);
    if (r1 != r2) f[r1] = r2;
  }
  int l = 0;
  for (int i = 1; i <= n + m; i++) {
    y = fa(i);
    if (!g[y]) {
      l++;
      g[y] = 1;
    }
  }
  printf("%d\n", l - 1);
  return 0;
}
