#include <bits/stdc++.h>
#pragma GCC optimize("03")
using namespace std;
int n, m, q, x[200100], y[200100], dad[400100];
bool viz[400100];
int find(int p) {
  if (dad[p] == p) return p;
  return dad[p] = find(dad[p]);
}
void join(int a, int b) { dad[find(a)] = find(b); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; i++) dad[i] = i;
  for (int i = 1; i <= q; i++) cin >> x[i] >> y[i];
  for (int i = 1; i <= q; i++) join(x[i], y[i] + n);
  int rs = 0;
  for (int i = 1; i <= n + m; i++) {
    int p = find(i);
    if (!viz[p]) rs++;
    viz[p]++;
  }
  cout << rs - 1;
  return 0;
}
