#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5, M = 1e6, P = 1e9 + 7;
int n, m, q, vis[N], ans;
std::vector<int> g[N];
void dfs(int u) {
  vis[u] = 1;
  for (auto v : g[u]) {
    if (!vis[v]) dfs(v);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> q;
  for (int i = 1, u, v; i <= q; i++) {
    cin >> u >> v;
    g[u].push_back(v + n), g[v + n].push_back(u);
  }
  for (int i = 1; i <= n + m; i++) {
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1 << '\n';
  return 0;
}
