#include <bits/stdc++.h>
using namespace std;
int m, n, q, num = -1;
int r, c;
queue<int> Q;
int main() {
  cin >> n >> m >> q;
  vector<vector<int>> v(n + m);
  vector<int> a(n + m, -1);
  for (int i = 0; i < q; i++) {
    cin >> r >> c;
    v[r - 1].push_back(n + c - 1);
    v[n + c - 1].push_back(r - 1);
  }
  for (int i = 0; i < v.size(); i++) {
    if (a[i] == -1) {
      num++;
      a[i] = num;
      Q.push(i);
      while (!Q.empty()) {
        r = Q.front();
        Q.pop();
        for (int j = 0; j < v[r].size(); j++) {
          if (a[v[r][j]] == -1) {
            a[v[r][j]] = num;
            Q.push(v[r][j]);
          }
        }
      }
    }
  }
  cout << num;
  return 0;
}
