#include <bits/stdc++.h>
using namespace std;
vector<int> G[400000];
bool used[400000];
void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < (int)G[v].size(); ++i) {
    if (!used[G[v][i]]) dfs(G[v][i]);
  }
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b + n);
    G[b + n].push_back(a);
  }
  int ans = 0;
  for (int i = 0; i < n + m; ++i) {
    if (used[i] == false) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1;
  return 0;
}
