#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = 1e17;
vector<vector<long long>> g;
vector<long long> used;
void dfs(long long v) {
  used[v] = 1;
  for (auto u : g[v]) {
    if (!used[u]) {
      dfs(u);
    }
  }
}
void solve() {
  long long n, m;
  cin >> n >> m;
  long long q;
  cin >> q;
  long long N = n + m;
  g.resize(N);
  used.resize(N);
  while (q--) {
    long long x, y;
    cin >> x >> y;
    --x, --y;
    x += m;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  long long ans = -1;
  for (long long i = 0; i < N; ++i) {
    if (!used[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt = 1;
  while (tt--) {
    solve();
    cout << '\n';
  }
}
