#include <bits/stdc++.h>
using namespace std;
vector<int> p;
int find(int x) {
  if (p[x] == x) {
    return x;
  }
  return p[x] = find(p[x]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  p = vector<int>(n + m);
  for (int i = 0; i < n + m; i++) {
    p[i] = i;
  }
  auto join = [=](int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b) {
      return false;
    }
    if (rand() & 1) {
      swap(a, b);
    }
    p[a] = b;
    return true;
  };
  int ans = n + m;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    y += n;
    if (join(x, y)) {
      ans--;
    }
  }
  ans--;
  cout << ans << endl;
}
