#include <bits/stdc++.h>
using namespace std;
const int maxn = 400001;
int n, m, q;
int mark[maxn];
vector<int> g[maxn];
void dfs(int now) {
  mark[now] = 1;
  int size = g[now].size();
  for (int i = 0; i < g[now].size(); i++) {
    if (mark[g[now][i]]) continue;
    dfs(g[now][i]);
  }
}
int main() {
  cin >> n >> m >> q;
  int r, c;
  for (int i = 1; i <= q; i++) {
    cin >> r >> c;
    c = c + n;
    g[r].push_back(c);
    g[c].push_back(r);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++) {
    if (mark[i]) continue;
    dfs(i);
    ans++;
  }
  cout << ans - 1 << '\n';
  return 0;
}
