#include <bits/stdc++.h>
using namespace std;
long long n, m, q;
vector<long long> a[400005];
bool fre[400005];
void dfs(long long u) {
  fre[u] = false;
  for (int i = 0; i < a[u].size(); i++) {
    long long v = a[u][i];
    if (fre[v]) dfs(v);
  }
}
int main() {
  memset(fre, true, sizeof fre);
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    long long u, v;
    cin >> u >> v;
    a[u].push_back(v + n);
    a[v + n].push_back(u);
  }
  long long sl = 0;
  for (int i = 1; i <= n + m; i++)
    if (fre[i]) dfs(i), sl++;
  cout << sl - 1;
  return 0;
}
