#include <bits/stdc++.h>
using namespace std;
long long rr[] = {0, 1, 1, 1, 0, -1, -1, -1};
long long cc[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long mod = 1e9 + 7;
const long long N = 400050;
vector<vector<long long> > adj(N);
vector<long long> vis(N);
void dfs(long long node) {
  vis[node] = 1;
  for (long long child : adj[node]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long test = 1;
  while (test--) {
    long long n, m, q;
    cin >> n >> m >> q;
    for (long long i = 0; i < q; i++) {
      long long x, y;
      cin >> x >> y;
      adj[x].push_back(n + y);
      adj[n + y].push_back(x);
    }
    long long cnt = 0;
    for (long long i = 1; i <= n + m; i++) {
      if (!vis[i]) {
        cnt++;
        dfs(i);
      }
    }
    cout << cnt - 1 << '\n';
  }
  return 0;
}
