#include <bits/stdc++.h>
using namespace std;
int n, m, q, ans = 0, i, x, y, dsu[400005];
int get(int p) { return dsu[p] == p ? p : dsu[p] = get(dsu[p]); }
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (i = 1; i <= n + m; i++) dsu[i] = i;
  for (i = 1; i <= q; i++) {
    scanf("%d%d", &x, &y);
    dsu[get(x)] = get(n + y);
  }
  for (i = 1; i <= n + m; i++)
    if (dsu[i] == i) ans++;
  printf("%d", --ans);
  return 0;
}
