#include <bits/stdc++.h>
using namespace std;
const int N = 400400;
int n, m, k;
vector<int> g[N];
bool used[N];
void dfs(int v) {
  used[v] = 1;
  for (int u : g[v]) {
    if (used[u]) continue;
    dfs(u);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  while (k--) {
    int v, u;
    scanf("%d%d", &v, &u);
    v--;
    u += n - 1;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  int ans = -1;
  for (int v = 0; v < n + m; v++) {
    if (used[v]) continue;
    dfs(v);
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
