#include <bits/stdc++.h>
using namespace std;
const long long maxn = 400000 + 200;
vector<long long> adj[maxn];
bool seen[maxn];
long long cnt;
void dfs(long long root) {
  seen[root] = 1;
  for (auto x : adj[root])
    if (!seen[x]) dfs(x);
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, q;
  cin >> n >> m >> q;
  for (long long(i) = (0); (i) < (q); (i)++) {
    long long x, y;
    cin >> x >> y;
    x--, y--;
    y += 200000;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (long long(i) = (0); (i) < (n); (i)++)
    if (!seen[i]) dfs(i), cnt++;
  for (long long(i) = (0); (i) < (m); (i)++)
    if (!seen[i + 200000]) dfs(i + 200000), cnt++;
  cout << cnt - 1 << "\n";
  return 0;
}
