#include <bits/stdc++.h>
using namespace std;
int sz[1000005], par[1000005];
int N, M, Q;
void init(int size) {
  for (int i = 0; i < size; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}
int find(int x) {
  while (x != par[x]) {
    par[x] = par[par[x]];
    x = par[x];
  }
  return (x);
}
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (sz[x] > sz[y]) {
    par[y] = x;
    sz[x] += sz[y];
  } else {
    par[x] = y;
    sz[y] += sz[x];
  }
}
int main() {
  cin >> N >> M >> Q;
  init(N + M + 1);
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    merge(a, b + N);
  }
  int ans = 0;
  for (int i = 0; i < (N + M); i++)
    if (find(i) == i) ans++;
  cout << ans - 1 << endl;
}
