#include <bits/stdc++.h>
using namespace std;
int father[400500], v[400500];
int fa(int x) {
  if (father[x] == x) return x;
  return father[x] = fa(father[x]);
}
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) father[i] = i;
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int fx = fa(x), fy = fa(n + y);
    if (fx != fy) father[fx] = fy;
  }
  long long ans = 0;
  for (int i = 1; i <= n + m; i++)
    if (!v[fa(i)]) ans++, v[fa(i)] = 1;
  printf("%I64d", ans - 1);
  return 0;
}
