#include <bits/stdc++.h>
int n, m, q;
std::vector<int> adj[400005];
bool visited[400005];
void dfs(int u) {
  visited[u] = true;
  for (auto v : adj[u]) {
    if (!visited[v]) {
      dfs(v);
    }
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 1; i <= q; i++) {
    int r, c;
    scanf("%d %d", &r, &c);
    int n_r = r, n_c = n + c;
    adj[n_r].push_back(n_c);
    adj[n_c].push_back(n_r);
  }
  int ans = -1;
  for (int i = 1; i <= n + m; i++) {
    if (!visited[i]) {
      dfs(i);
      ans++;
    }
  }
  printf("%d\n", ans);
}
