#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int f[400010 + 1];
void init() {
  for (int i = 1; i <= 400010; i++) {
    f[i] = i;
  }
}
int getf(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = getf(f[x]);
}
void merge(int a, int b) {
  int t1 = getf(a);
  int t2 = getf(b);
  if (t1 != t2) {
    f[t1] = t2;
  }
}
int main() {
  int ans = 0;
  scanf("%d%d%d", &n, &m, &q);
  init();
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    merge(a, b + n);
  }
  for (int i = 1; i <= n + m; i++)
    if (f[i] == i) ans++;
  printf("%d\n", ans - 1);
}
