#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, mod2 = 998244353;
unsigned long long base = 131;
const int w = 4e5 + 5;
int n, m, k;
int f[w];
int get(int x) {
  if (x != f[x]) f[x] = get(f[x]);
  return f[x];
}
int main() {
  int i, j, t;
  int q;
  cin >> n >> m >> q;
  for (i = 1; i <= n + m; i++) f[i] = i;
  int ans = n + m - 1;
  while (q--) {
    int x, y;
    scanf("%d %d", &x, &y);
    y += n;
    int fx = get(x), fy = get(y);
    if (fx != fy) {
      f[fx] = fy;
      ans--;
    }
  }
  printf("%d\n", ans);
  return 0;
}
