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