#include <bits/stdc++.h>
using namespace std;
int get() {
  char ch;
  while (!((ch = getchar() - 48) >= 0))
    ;
  int v = ch;
  while (((ch = getchar() - 48) >= 0)) v = (v << 1) + (v << 3) + ch;
  return v;
}
int n, m, q, x, y, cnt, head[200005 << 1];
bool f[200005 << 1];
struct node {
  int to, next;
} e[200005 << 1];
void insert(int u, int v) {
  e[++cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
  e[++cnt].to = u;
  e[cnt].next = head[v];
  head[v] = cnt;
}
void dfs(int x) {
  f[x] = 1;
  for (int i = head[x]; i; i = e[i].next)
    if (!f[e[i].to]) dfs(e[i].to);
}
int main() {
  n = get();
  m = get();
  q = get();
  for (int i = 1; i <= q; i++) x = get(), y = get(), insert(x, y + n);
  int ans = 0;
  for (int i = 1; i <= n + m; i++)
    if (!f[i]) ans++, dfs(i);
  printf("%d\n", ans - 1);
  return 0;
}
