#include <bits/stdc++.h>
using namespace std;
int n, m, q, fa[1000010], siz[1000010];
void init(int xx) {
  for (int i = 1; i <= xx; i++) {
    fa[i] = i;
    siz[i] = 1;
  }
  return;
}
int find(int xx) {
  if (fa[xx] == xx) return xx;
  fa[xx] = find(fa[xx]);
  return fa[xx];
}
void merge(int xx, int yy) {
  int fx = find(xx), fy = find(yy);
  if (fx == fy) return;
  if (siz[fx] < siz[fy]) {
    fa[fx] = fy;
    siz[fy] += siz[fx];
    fa[xx] = fa[yy] = fy;
    return;
  } else {
    fa[fy] = fx;
    siz[fx] += siz[fy];
    fa[xx] = fa[yy] = fx;
    return;
  }
}
int getcnt(int xx) {
  int ans = 0;
  for (int i = 1; i <= xx; i++)
    if (fa[i] == i) ans++;
  return ans;
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  init(n + m);
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    merge(x, y + n);
  }
  printf("%d\n", getcnt(n + m) - 1);
  return 0;
}
