#include <bits/stdc++.h>
using namespace std;
int n, m, q, fa[210000 << 1], ans;
int getfa(int x) {
  if (x != fa[x]) {
    fa[x] = getfa(fa[x]);
  }
  return fa[x];
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  ans = n + m;
  for (int i = 1; i <= n + m; i++) fa[i] = i;
  for (int i = 1; i <= q; i++) {
    int r, c;
    scanf("%d%d", &r, &c);
    int f1 = getfa(r), f2 = getfa(c + n);
    if (f1 != f2) {
      fa[f1] = f2;
      ans--;
    }
  }
  printf("%d\n", ans - 1);
}
