#include <bits/stdc++.h>
using namespace std;
const int MAX = 4e5 + 50;
int par[MAX];
int Rank[MAX];
int Find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = Find(par[x]);
  }
}
void unit(int x, int y) {
  x = Find(x);
  y = Find(y);
  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y]) {
      Rank[x]++;
    }
  }
}
int main(int argc, char const *argv[]) {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) {
    par[i] = i;
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    unit(x, n + y);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++) {
    if (par[i] == i) {
      ans++;
    }
  }
  printf("%d\n", ans - 1);
  return 0;
}
