#include <bits/stdc++.h>
using namespace std;
const int maxn = (1e6) + 10;
int a, b, n, ans, id[maxn];
int find(int x) {
  if (id[x] == x) return x;
  return (id[x] = find(id[x]));
}
int main() {
  scanf("%d %d %d", &a, &b, &n);
  for (int i = 1; i <= a + b; i++) id[i] = i;
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    id[find(x)] = find(a + y);
  }
  for (int i = 1; i <= a + b; i++)
    if (find(i) == i) ans++;
  printf("%d\n", ans - 1);
  return 0;
}
