#include <bits/stdc++.h>
using namespace std;
int N, M, Q, ans = -1;
bool vis[400007];
vector<int> adj[400007];
void DFS(int x) {
  vis[x] = 1;
  for (auto i : adj[x])
    if (!vis[i]) DFS(i);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> M >> Q;
  for (int i = 0, r, c; i < Q; i++) {
    cin >> r >> c;
    adj[r].push_back(c + N);
    adj[c + N].push_back(r);
  }
  for (int i = 1; i <= N + M; i++)
    if (!vis[i]) {
      DFS(i);
      ans++;
    }
  cout << ans << endl;
}
