#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long fa[N << 1];
long long find(int x) { return fa[x] == 0 ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m, q;
  scanf("%d %d %d", &n, &m, &q);
  int x, y;
  int ans = n + m - 1;
  while (q--) {
    scanf("%d %d", &x, &y);
    y = y + n;
    x = find(x);
    y = find(y);
    if (x != y) {
      ans--;
      fa[x] = y;
    }
  }
  printf("%d\n", ans);
}
