#include <bits/stdc++.h>
using namespace std;
vector<long long int> v[1000000];
map<long long int, long long int> vis;
void dfs(long long int c) {
  vis[c] = 1;
  for (long long int i = 0; i < v[c].size(); i++) {
    if (vis[v[c][i]] == 0) {
      dfs(v[c][i]);
    }
  }
}
int main() {
  long long int n, m, k;
  cin >> n >> m >> k;
  long long int out = -1;
  while (k--) {
    long long int x, y;
    cin >> x >> y;
    v[x].push_back(y + n);
    v[y + n].push_back(x);
  }
  for (long long int i = 1; i <= n + m; i++) {
    if (vis[i] == 0) {
      dfs(i);
      out++;
    }
  }
  if (out == -1) {
    out++;
  }
  cout << out;
  return 0;
}
