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