#include <bits/stdc++.h>
using namespace std;
const int MN = 4e5 + 100;
vector<int> adj[MN];
bool mark[MN];
void dfs(int v) {
  mark[v] = 1;
  for (int u : adj[v]) {
    if (!mark[u]) {
      dfs(u);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y + n);
    adj[y + n].push_back(x);
  }
  int cnt = 0;
  for (int i = 0; i < n + m; i++) {
    if (!mark[i]) {
      dfs(i);
      cnt++;
    }
  }
  cout << cnt - 1;
}
