#include <bits/stdc++.h>
struct DSU {
  long long M[400005];
  DSU(long long n) {
    for (long long i = 0; i < n; i++) {
      M[i] = i;
    }
  }
  long long find(long long x) {
    while (M[x] != x) {
      long long p = M[x];
      M[x] = M[p];
      x = p;
    }
    return x;
  }
  bool join(long long x, long long y) {
    long long xr = find(x);
    long long yr = find(y);
    if (xr == yr) {
      return false;
    } else {
      M[xr] = yr;
      return true;
    }
  }
};
using namespace std;
int main() {
  long long n, m, q;
  cin >> n >> m >> q;
  DSU dsu(n + m);
  long long z = n + m - 1;
  for (long long i = 0; i < q; i++) {
    long long r, c;
    cin >> r >> c;
    r--;
    c--;
    if (dsu.join(r, c + n)) z--;
  }
  cout << z;
}
