#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e6 + 100;
const int maxm = 5e3 + 100;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int Scan() {
  int res = 0, ch, flag = 0;
  if ((ch = getchar()) == '-')
    flag = 1;
  else if (ch >= '0' && ch <= '9')
    res = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + ch - '0';
  return flag ? -res : res;
}
int pre[maxn];
int find(int x) {
  if (pre[x] == x)
    return x;
  else {
    pre[x] = find(pre[x]);
    return pre[x];
  }
}
int main() {
  int n, m, k;
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= n + m; i++) {
    pre[i] = i;
  }
  for (int i = 0; i < k; i++) {
    int x, y;
    x = Scan();
    y = Scan();
    int t1 = find(x);
    int t2 = find(y + n);
    if (t1 != t2) {
      pre[t1] = t2;
    }
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++) {
    if (pre[i] == i) ans++;
  }
  printf("%d", ans - 1);
  return 0;
}
