#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 7;
int N, M, Q;
int p[MAXN];
int Find(int x) { return p[x] = p[x] == x ? x : Find(p[x]); }
int main() {
  scanf("%d%d%d", &N, &M, &Q);
  for (int i = (1); i <= (N + M); i++) p[i] = i;
  for (int i = (1); i <= (Q); i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    y += N;
    int fx = Find(x), fy = Find(y);
    if (fx ^ fy) {
      p[fx] = fy;
    }
  }
  int cnt = 0;
  for (int i = (1); i <= (N + M); i++) cnt += (Find(i) == i);
  printf("%d\n", cnt - 1);
  return 0;
}
