#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, x, y, z;
int f[400010], ans, q;
int getint() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret;
}
int find(int x) { return f[x] == x ? f[x] : f[x] = find(f[x]); }
int main() {
  cin >> n >> m >> q;
  for (i = 1; i <= n + m; i++) f[i] = i;
  for (i = 1; i <= q; i++) {
    x = getint(), y = getint() + n;
    f[find(x)] = find(y);
  }
  for (i = 1; i <= n + m; i++)
    if (f[i] == i) ans++;
  cout << ans - 1;
  return 0;
}
