#include <bits/stdc++.h>
using namespace std;
int n, m, q, cnt, f[400005];
int fnd(int x) { return f[x] == 0 ? x : f[x] = fnd(f[x]); }
int main() {
  scanf("%d%d%d", &n, &m, &q);
  cnt = n + m - 1;
  for (int i = 1, x, y; i <= q; i++) {
    scanf("%d%d", &x, &y);
    y += n;
    x = fnd(x);
    y = fnd(y);
    if (x != y) {
      cnt--;
      f[x] = y;
    }
  }
  printf("%d\n", cnt);
  return 0;
}
