#include <bits/stdc++.h>
using namespace std;
vector<int> used;
vector<vector<int> > gr;
void dfs(int v) {
  used[v] = 1;
  for (int to : gr[v]) {
    if (!used[to]) dfs(to);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  used.resize(n + m);
  gr.resize(n + m);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    gr[x].push_back(y + n);
    gr[y + n].push_back(x);
  }
  int ans = 0;
  for (int i = 0; i < n + m; i++) {
    if (!used[i]) {
      ans++;
      dfs(i);
    }
  }
  cout << ans - 1 << "\n";
  return 0;
}
