#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long x = 0, o = 1;
  register char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = ((x << 3) + (x << 1)) + ch - '0', ch = getchar();
  return x * o;
}
int n, m, fa[201000 << 1], k, vis[201000 << 1];
inline int Find(int x) {
  if (fa[x] != x) fa[x] = Find(fa[x]);
  return fa[x];
}
int main() {
  n = read(), m = read(), k = read();
  for (register int i = 1; i <= n + m; ++i) fa[i] = i;
  for (register int i = 1; i <= k; ++i) {
    int x = read(), y = read() + n;
    int fx = Find(x), fy = Find(y);
    if (fx != fy) fa[fx] = fy;
  }
  int ans = 0;
  for (register int i = 1; i <= n + m; ++i) {
    int now = Find(i);
    if (!vis[now]) vis[now] = 1, ++ans;
  }
  printf("%d\n", ans - 1);
}
