#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m, q;
int pre[maxn << 1];
int Find(int x) { return x == pre[x] ? x : pre[x] = Find(pre[x]); }
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) pre[i] = i;
  int a, b, ans = n + m;
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &a, &b);
    int x = Find(a), y = Find(b + n);
    if (x != y) {
      pre[x] = y;
      ans--;
    }
  }
  printf("%d\n", ans - 1);
  return 0;
}
