#include <bits/stdc++.h>
using namespace std;
long long ksm(long long a, long long b) {
  long long ns = ksm(a, b >> 1);
  ns = ns * ns % 998244353;
  if (b & 1) ns = ns * a % 998244353;
  return ns;
}
int fa[400005];
int gfa(int a) {
  if (fa[a] == a) return a;
  return fa[a] = gfa(fa[a]);
}
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) fa[i] = i;
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    fa[gfa(y + n)] = gfa(x);
  }
  int tot = 0;
  for (int i = 1; i <= n + m; i++)
    if (fa[i] == i) tot++;
  printf("%d\n", tot - 1);
  return 0;
}
