#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<int> was;
void dfs(int v) {
  was[v] = 1;
  for (auto &u : g[v])
    if (!was[u]) dfs(u);
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  g.resize(n + m);
  was.resize(n + m);
  int a, b;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(n + b);
    g[n + b].push_back(a);
  }
  int ans = 0;
  for (int i = 0; i < n + m; i++) {
    if (!was[i]) {
      ans++;
      dfs(i);
    }
  }
  cout << ans - 1;
  return 0;
}
