#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;
int n, m, q, r, c;
bool vis[400001];
vector<int> adj[400001];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> r >> c;
    adj[r].push_back(c + n);
    adj[c + n].push_back(r);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++) {
    if (!vis[i]) {
      ans++;
      queue<int> q;
      q.push(i);
      vis[i] = true;
      while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (int v : adj[cur]) {
          if (!vis[v]) {
            vis[v] = true;
            q.push(v);
          }
        }
      }
    }
  }
  cout << ans - 1 << "\n";
  return 0;
}
