#include <bits/stdc++.h>
using namespace std;
const int MAXN = 400000;
int cntR[MAXN];
int cntC[MAXN];
vector<int> xs[MAXN];
int p[MAXN];
int cntComp = 0;
int getRoot(int x) {
  if (p[x] == x) {
    return x;
  }
  int res = getRoot(p[x]);
  p[x] = res;
  return res;
}
void join(int x, int y) {
  int r1 = getRoot(x);
  int r2 = getRoot(y);
  if (r1 != r2) {
    cntComp--;
    p[r1] = r2;
  }
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  cntComp = n;
  for (int i = 0; i < (int)(n); i++) {
    p[i] = i;
  }
  int freeCols = m;
  for (int i = 0; i < (int)(q); i++) {
    int x, y;
    cin >> x >> y;
    xs[--y].push_back(--x);
    cntR[x]++;
    cntC[y]++;
    if (cntC[y] == 1) {
      freeCols--;
    }
  }
  for (int i = 0; i < (int)(m); i++) {
    for (int j = (int)1; j <= (int)(xs[i].size() - 1); j++) {
      join(xs[i][0], xs[i][j]);
    }
  }
  cout << cntComp - 1 + freeCols;
  return 0;
}
