#include <bits/stdc++.h>
using namespace std;
const int maxn = 400002;
int n, m, z, pre[maxn], num, x2, y2;
int finds(int x) { return x == pre[x] ? x : pre[x] = finds(pre[x]); }
int unions(int x, int y) {
  int x1 = finds(x);
  int y1 = finds(y);
  if (x1 != y1) {
    pre[y1] = x1;
    return 1;
  }
  return 0;
}
int main() {
  scanf("%d%d%d", &n, &m, &z);
  num = n + m - 1;
  for (int i = 1; i <= n + m; i++) pre[i] = i;
  for (int i = 1; i <= z; i++) {
    scanf("%d%d", &x2, &y2);
    if (unions(x2, y2 + n) == 1) num--;
  }
  printf("%d\n", num);
  return 0;
}
