#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int n, m, q, ans;
bool v[maxn];
vector<int> g[maxn];
void dfs(int x) {
  if (v[x]) return;
  v[x] = true;
  for (int y : g[x]) dfs(y);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> q;
  int x, y;
  while (q--) {
    cin >> x >> y;
    y += n;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n + m; i++)
    if (!v[i]) {
      dfs(i);
      ans++;
    }
  cout << ans - 1 << endl;
  return 0;
}
