#include <bits/stdc++.h>
using namespace std;
int f[400100];
int fa(int x) {
  int y = x;
  while (x != f[x]) x = f[x];
  while (y != f[y]) {
    int tmp = f[y];
    f[y] = x;
    y = tmp;
  }
  return x;
}
int main() {
  int n, m, k, ans;
  cin >> n >> m >> k;
  ans = n + m - 1;
  for (int i = 1; i <= n + m; ++i) f[i] = i;
  for (int i = 1; i <= k; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    y += n;
    int fx = fa(x), fy = fa(y);
    if (fx != fy) f[fx] = fy, --ans;
  }
  printf("%d\n", ans);
  return 0;
}
