#include <bits/stdc++.h>
using namespace std;
int parent[200005];
int find(int x) {
  if (parent[x] == x) return x;
  return parent[x] = find(parent[x]);
}
void join(int x, int y) {
  int p = find(x);
  int q = find(y);
  if (p == q) return;
  parent[p] = q;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> X[n + 1], Y[m + 1];
  set<int> idx, gp;
  for (int i = (int)(1); i <= (int)(n); i++) parent[i] = i;
  for (int i = (int)(1); i <= (int)(q); i++) {
    int x, y;
    cin >> x >> y;
    X[x].push_back(y);
    Y[y].push_back(x);
    idx.insert(y);
  }
  for (int i = (int)(1); i <= (int)(m); i++)
    for (int j = (int)(1); j <= (int)(Y[i].size() - 1); j++)
      join(Y[i][j - 1], Y[i][j]);
  for (int i = (int)(1); i <= (int)(n); i++) gp.insert(find(i));
  cout << gp.size() - 1 + (m - idx.size());
}
