#include <bits/stdc++.h>
const int N = 1e6 + 7;
using namespace std;
int n, m, q, vis[N];
vector<int> g[N];
void add(int u, int v) { g[u].push_back(v), g[v].push_back(u); }
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    if (!vis[v]) dfs(v);
  }
  return;
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= q; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y + n);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; ++i)
    if (!vis[i]) {
      ++ans;
      dfs(i);
    }
  printf("%d\n", ans - 1);
  return 0;
}
