#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 5e5 + 5;
int par[N];
int n, m, q;
int getpar(int u) {
  if (par[par[u]] == par[u]) return par[u];
  return par[u] = getpar(par[u]);
}
void unite(int a, int b) {
  a = getpar(a);
  b = getpar(b);
  par[a] = b;
}
int main() {
  iota(par, par + N, 0);
  cin >> n >> m >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    unite(a, b + n);
  }
  int ans = -1;
  for (int i = 1; i <= n + m; ++i)
    if (getpar(i) == i) ++ans;
  cout << ans << '\n';
}
