#include <bits/stdc++.h>
using namespace std;
const long long N = 1000005;
long long n, m, k, t1, t2, fa[N];
long long find(long long p) { return p == fa[p] ? p : fa[p] = find(fa[p]); }
void merge(long long p, long long q) {
  p = find(p);
  q = find(q);
  if (p - q) fa[p] = q;
}
signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  for (long long i = 1; i <= n + m; i++) fa[i] = i;
  for (long long i = 1; i <= k; i++) {
    cin >> t1 >> t2;
    merge(t1, t2 + n);
  }
  long long ans = 0;
  for (long long i = 1; i <= n + m; i++)
    if (find(i) == i) ++ans;
  cout << ans - 1;
}
