#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int fa[maxn];
int m, n, q, i, x, y;
int getfa(int x) {
  if (x == fa[x]) return x;
  return fa[x] = getfa(fa[x]);
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (i = 1; i <= m + n; i++) fa[i] = i;
  for (i = 1; i <= q; i++) {
    scanf("%d%d", &x, &y);
    fa[getfa(x)] = getfa(y + n);
  }
  int num = 0;
  for (i = 1; i <= m + n; i++) {
    if (fa[i] == i) num++;
  }
  cout << num - 1 << endl;
}
