#include <bits/stdc++.h>
using namespace std;
const int sz = 200010;
int p[2 * sz];
int get(int n) {
  if (p[n] == 0) {
    return n;
  } else {
    p[n] = get(p[n]);
    return p[n];
  }
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  int ans = n + m - 1;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    int px = get(x), py = get(y + n);
    if (px != py) {
      ans--;
      p[px] = py;
    }
  }
  cout << ans << endl;
  return 0;
}
