#include <bits/stdc++.h>
using namespace std;
int n, m, q, ans, top[200005 << 1];
int find(int u) {
  if (top[u] == u) return u;
  return top[u] = find(top[u]);
}
inline void work() {
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) top[i] = i;
  for (int i = 1, u, v; i <= q; i++) {
    scanf("%d %d", &u, &v);
    v = v + n;
    int fu = find(u), fv = find(v);
    if (fu == fv) continue;
    top[fu] = fv;
  }
  for (int i = 1; i <= n + m; i++)
    if (top[i] == i) ans++;
  printf("%d", ans - 1);
}
int main() {
  work();
  return 0;
}
