#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int n, m, q, ans, rt[N];
int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x;
}
int root(int u) { return rt[u] == u ? u : rt[u] = root(rt[u]); }
void add(int u, int v) {
  if (root(u) != root(v)) rt[rt[u]] = rt[v];
}
int main() {
  n = read(), m = read(), q = read();
  for (int i = 1; i <= n + m; i++) rt[i] = i;
  for (int u, v; q; q--) u = read(), v = read(), add(u, v + n);
  for (int i = 1; i <= n + m; i++) ans += root(i) == i;
  printf("%d", ans - 1);
}
