#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 10;
vector<int> graph[MAXN];
bool vis[MAXN];
void dfs(int curr, int par) {
  if (vis[curr]) {
    return;
  }
  vis[curr] = true;
  for (int i = 0; i < graph[curr].size(); i++) {
    int u = graph[curr][i];
    if (u == par) {
      continue;
    }
    dfs(u, curr);
  }
  return;
}
int main(int argc, char const *argv[]) {
  int n, m, q;
  cin >> n >> m >> q;
  memset(vis, 0, sizeof(vis));
  while (q--) {
    int r, c;
    cin >> r >> c;
    r--;
    c--;
    graph[r].push_back(c + n);
    graph[c + n].push_back(r);
  }
  int connectedComponent = 0;
  for (int i = 0; i < n + m; i++) {
    if (vis[i]) {
      continue;
    }
    dfs(i, -1);
    connectedComponent++;
  }
  cout << connectedComponent - 1;
  return 0;
}
