#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 7;
int vis[N];
struct ee {
  int to, next;
} e[N];
int head[N];
int cnt;
void ins(int u, int v) {
  e[++cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void dfs(int x) {
  vis[x] = 1;
  for (int i = head[x]; i; i = e[i].next) {
    int v = e[i].to;
    if (vis[v]) continue;
    dfs(v);
  }
}
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= q; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ins(u, v + n);
    ins(v + n, u);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++) {
    if (!vis[i]) {
      ans++;
      dfs(i);
    }
  }
  printf("%d", ans - 1);
}
