#include <bits/stdc++.h>
using namespace std;
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
int ans = -1;
vector<vector<int> > adj[2];
vector<int> visited[2];
void dfs(int node, int type) {
  visited[type][node] = 1;
  for (int x : adj[type][node]) {
    if (!visited[1 - type][x]) dfs(x, 1 - type);
  }
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  adj[0].resize(n + 1);
  adj[1].resize(m + 1);
  visited[1].assign(m + 1, 0);
  visited[0].assign(n + 1, 0);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    adj[0][x].push_back(y);
    adj[1][y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    if (!visited[0][i]) {
      ans++;
      dfs(i, 0);
    }
  for (int i = 1; i <= m; i++)
    if (!visited[1][i]) {
      ans++;
      dfs(i, 1);
    }
  cout << ans;
}
