#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 600010;
const int INF = 2147483600;
int fa[MAXN + 1];
int N, M, Q;
inline int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }
inline void merge(int u, int v) {
  int x = find(u), y = find(v);
  if (x == y) return;
  fa[y] = x;
}
int main() {
  N = read(), M = read(), Q = read();
  int cnt = 0;
  for (int i = 1; i <= N + M; i++) fa[i] = i;
  for (int i = 1; i <= Q; i++) {
    int u = read(), v = read();
    merge(u, v + N);
  }
  for (int i = 1; i <= N + M; i++)
    if (fa[i] == i) ++cnt;
  printf("%d\n", cnt - 1);
  return 0;
}
