#include <bits/stdc++.h>
const int maxn = 400000 + 5;
int f[maxn];
int n, m, q;
int ans;
inline int Read() {
  register int ret;
  register char r;
  register bool f = false;
  while (r = getchar(), r < '0' || r > '9')
    if (r == '-') f = true;
  ret = r - 48;
  while (r = getchar(), r >= '0' && r <= '9')
    ret = (ret << 3) + (ret << 1) + r - 48;
  return f ? ~ret + 1 : ret;
}
inline int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); }
int main() {
  n = Read();
  m = Read();
  q = Read();
  for (register int i = n + m; i; i--) f[i] = i;
  for (register int i = 1; i <= q; i++) {
    int x = Find(Read()), y = Find(Read() + n);
    if (x != y) f[x] = y;
  }
  for (register int i = n + m; i; i--) ans += (Find(i) == i);
  printf("%d\n", ans - 1);
  return 0;
}
