#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000, MAXM = 200000, MAXQ = 200000;
int N, M, Q;
int X[MAXQ + 1], Y[MAXQ + 1];
int par[MAXN + MAXM + 1];
int ans;
void init() {
  scanf("%d %d %d", &N, &M, &Q);
  for (int i = 1; i <= Q; ++i) scanf("%d %d", &X[i], &Y[i]);
  for (int i = 1; i <= N + M; ++i) par[i] = i;
}
int Find(int x) { return x == par[x] ? x : par[x] = Find(par[x]); }
void solve() {
  ans = N + M - 1;
  for (int i = 1; i <= Q; ++i) {
    int u = X[i], v = Y[i] + N;
    int p = Find(u), q = Find(v);
    if (p != q) {
      ans--;
      par[p] = q;
    }
  }
  cout << ans << endl;
}
int main() {
  init();
  solve();
  return 0;
}
