#include <bits/stdc++.h>
using namespace std;
long long vis[400400];
std::vector<long long> adj[400400];
void dfs(long long x) {
  vis[x] = 1;
  for (auto itr : adj[x]) {
    if (!vis[itr]) {
      dfs(itr);
    }
  }
}
int main() {
  long long n, m, i, q, x, y;
  cin >> n >> m >> q;
  for (i = 0; i < q; i++) {
    cin >> x >> y;
    adj[x].push_back(y + n);
    adj[y + n].push_back(x);
  }
  long long ans = -1;
  for (i = 1; i <= n + m; i++) {
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans;
  return 0;
}
