#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int pre[N << 1];
int find(int x) {
  if (!pre[x]) return x;
  return pre[x] = find(pre[x]);
}
int main() {
  ios::sync_with_stdio(false);
  int n, m, r, x, y;
  cin >> n >> m >> r;
  int ans = n + m - 1;
  while (r--) {
    cin >> x >> y;
    x = find(x);
    y = find(y + n);
    if (x == y) continue;
    ans--;
    pre[x] = y;
  }
  cout << ans << endl;
  return 0;
}
