#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const long long inf = 0x3f3f3f3f;
inline long long mul(long long x, long long y, long long mod) {
  long long res = (x * y - (long long)(long double)(x / mod * y + 1e-8) * mod);
  return res < 0 ? res + mod : res;
}
const double PI = acos(-1.0);
const int mod = 1e9 + 7;
long long qPow(long long base, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * base);
    base = (base * base);
    n >>= 1;
  }
  return res;
}
int pre[2 * maxn];
void init() { memset(pre, -1, sizeof(pre)); }
int Find(int x) { return pre[x] == -1 ? x : pre[x] = Find(pre[x]); }
void Union(int x, int y) {
  x = Find(x);
  y = Find(y);
  if (x != y) pre[x] = y;
}
int main() {
  init();
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(x, y + n);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++)
    if (pre[i] == -1) ans++;
  printf("%d\n", ans - 1);
  return 0;
}
