#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int n, m, k, ds[MX], rnk[MX];
struct cell {
  int r, c;
  bool operator<(const cell &o) const { return c != o.c ? c < o.c : r < o.r; }
} q[MX];
int find(int x) { return x == ds[x] ? x : ds[x] = find(ds[x]); }
bool merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return false;
  if (rnk[x] < rnk[y])
    ds[x] = y;
  else {
    if (rnk[x] == rnk[y]) ++rnk[x];
    ds[y] = x;
  }
  return true;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < k; ++i) {
    scanf("%d %d", &q[i].r, &q[i].c);
  }
  for (int i = 1; i <= n; ++i) ds[i] = i;
  sort(q, q + k);
  int took = 0;
  for (int i = 0; i < k;) {
    int j = i + 1;
    int r1 = q[i].r;
    ++took;
    while (j < k && q[i].c == q[j].c) {
      int r2 = q[j].r;
      assert(r1 != r2);
      if (merge(r1, r2)) {
        ++took;
        r1 = r2;
      }
      ++j;
    }
    i = j;
  }
  printf("%d\n", n + m - 1 - took);
}
