#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
vector<pii> elements;
vector<int> adj[400001];
bool visited[400001];
void dfs(int u) {
  visited[u] = true;
  for (int v : adj[u]) {
    if (not visited[v]) {
      dfs(v);
    }
  }
}
long long answer(int n, int m) {
  int components = 0;
  for (int i = 1; i <= n + m; ++i) {
    if (not visited[i]) {
      dfs(i);
      components++;
    }
  }
  return components - 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, q;
  cin >> n >> m >> q;
  int r, c;
  for (int i = 0; i < q; ++i) {
    cin >> r >> c;
    c += n;
    adj[r].push_back(c);
    adj[c].push_back(r);
  }
  cout << answer(n, m);
  return 0;
}
