#include <bits/stdc++.h>
int fa[400005];
int find(int x) {
  if (x == fa[x]) return x;
  fa[x] = find(fa[x]);
  return (fa[x]);
}
int main() {
  int ans, n, m, k, p, q, x, y, i;
  scanf("%d%d%d", &n, &m, &k);
  for (i = 1; i <= n + m; i++) fa[i] = i;
  for (i = 0; i < k; i++) {
    scanf("%d%d", &x, &y);
    p = find(x);
    q = find(y + n);
    if (p != q) fa[p] = q;
  }
  ans = 0;
  for (i = 1; i <= n + m; i++)
    if (find(i) == i) ans++;
  printf("%d", ans - 1);
  return 0;
}
