#include <bits/stdc++.h>
using namespace std;
const int __ = 600005;
struct DisjointSetUnion {
  int pre[__];
  DisjointSetUnion() { memset(pre, -1, sizeof(pre)); }
  void un(int x, int y) {
    x = fd(x), y = fd(y);
    if (x == y) return;
    if (pre[x] < pre[y])
      pre[x] += pre[y], pre[y] = x;
    else
      pre[y] += pre[x], pre[x] = y;
  }
  int fd(int x) {
    if (pre[x] < 0) return x;
    return pre[x] = fd(pre[x]);
  }
  void clear() { memset(pre, -1, sizeof(pre)); }
} dsu;
bool vis[__];
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = (1); i <= (q); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    dsu.un(x, y + n);
  }
  int ans = 0;
  for (int i = (1); i <= (n + m); ++i)
    if (dsu.fd(i) != dsu.fd(1)) dsu.un(i, 1), ++ans;
  printf("%d\n", ans);
  return 0;
}
