#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
vector<int> g[2 * N];
int used[2 * N];
void dfs(int v) {
  used[v] = 1;
  for (int to : g[v]) {
    if (!used[to]) dfs(to);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; (i) < (q); ++i) {
    int v, u;
    cin >> v >> u;
    v--;
    u--;
    g[v].push_back(N + u);
    g[N + u].push_back(v);
  }
  int cnt = 0;
  for (int i = 0; (i) < (n); ++i) {
    if (!used[i]) {
      dfs(i);
      cnt++;
    }
  }
  for (int i = 0; (i) < (m); ++i) {
    if (!used[N + i]) {
      dfs(N + i);
      cnt++;
    }
  }
  cout << cnt - 1 << '\n';
  return 0;
}
