#include <bits/stdc++.h>
using namespace std;
class DSU {
  int n;
  int* p;
  int* w;

 public:
  DSU(int _n) {
    n = _n;
    p = new int[n];
    w = new int[n];
    for (int i = 0; i < n; i++) {
      p[i] = i;
      w[i] = 1;
    }
  }
  int getNum(int a) {
    int b = a;
    while (p[b] != b) b = p[b];
    while (p[a] != a) {
      int t = p[a];
      p[a] = b;
      a = t;
    }
    return b;
  }
  void setUnion(int a, int b) {
    int p1 = getNum(a);
    int p2 = getNum(b);
    if (w[p1] <= w[p2]) {
      w[p2] += w[p1];
      p[p1] = p2;
    } else {
      w[p1] += w[p2];
      p[p2] = p1;
    }
  }
  bool isEqual(int a, int b) { return getNum(a) == getNum(b); }
  int getSize(int a) { return w[getNum(a)]; }
};
int main() {
  int m, n, q;
  cin >> n >> m >> q;
  DSU d(m + n);
  int ans = m + n - 1;
  for (int i = 0; i < q; i++) {
    int r, c;
    cin >> r >> c;
    r--;
    c--;
    if (!d.isEqual(c, m + r)) {
      d.setUnion(c, m + r);
      ans--;
    }
  }
  cout << ans;
  return 0;
}
