#include <bits/stdc++.h>
using namespace std;
set<int> col[200005];
int row[200005];
int pre[200005];
int pset[200005];
int findSet(int a) { return (a == pset[a]) ? a : pset[a] = findSet(pset[a]); }
bool used[200005];
int main() {
  int n, m, q;
  scanf("%d %d %d", &n, &m, &q);
  memset(row, 0, sizeof(row));
  for (int i = (1); i <= (q); i += (1)) {
    int x, y;
    scanf("%d %d", &x, &y);
    col[y].insert(x);
    row[x]++;
  }
  memset(pre, -1, sizeof(pre));
  for (int i = (1); i <= (m); i += (1)) pset[i] = i;
  for (int i = (1); i <= (m); i += (1)) {
    for (auto u : col[i]) {
      if (pre[u] != -1) {
        pset[findSet(i)] = findSet(pre[u]);
      }
      pre[u] = i;
    }
  }
  memset(used, 0, sizeof(used));
  int cnt = 0;
  for (int i = (1); i <= (m); i += (1)) {
    if (!used[findSet(i)]) {
      used[findSet(i)] = 1;
      cnt++;
    }
  }
  cnt--;
  for (int i = (1); i <= (n); i += (1)) {
    if (!row[i]) cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}
