#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 9;
long long par[N], cnt[N], ans, com;
vector<long long> v[N];
long long root(long long x) {
  while (par[x] != x) {
    par[x] = par[par[x]];
    x = par[x];
  }
  return x;
}
void uni(long long x, long long y) { par[root(x)] = par[root(y)]; }
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n, m, q;
  cin >> n >> m >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y;
    v[x].push_back(y);
  }
  for (long long i = (long long)(1); i <= (long long)(m); i++) par[i] = i;
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    if (v[i].size() == 0) ans++;
    if (v[i].size() < 2) continue;
    for (long long j = (long long)(1); j <= (long long)(v[i].size() - 1); j++) {
      if (root(v[i][j]) != root(v[i][j - 1])) uni(v[i][j], v[i][j - 1]);
    }
  }
  for (long long i = (long long)(1); i <= (long long)(m); i++) cnt[root(i)]++;
  for (long long i = (long long)(1); i <= (long long)(m); i++)
    if (cnt[i]) com++;
  ans += com - 1;
  cout << ans;
}
