#include <bits/stdc++.h>
using namespace std;
int parent[200005 * 2];
inline int Find(int a) {
  if (parent[a] == a) return a;
  parent[a] = Find(parent[a]);
  return parent[a];
}
inline void Union(int a, int b) { parent[Find(b)] = parent[Find(a)]; }
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; i <= n + m; i++) parent[i] = i;
  while (q--) {
    int a, b;
    cin >> a >> b;
    Union(a, n + b);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++) {
    if (parent[i] == i) ans++;
  }
  cout << ans - 1 << "\n";
  return 0;
}
