#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, el_nr;
  cin >> n >> m >> el_nr;
  vector<vector<int> > vertex___neigh_v(n + m);
  for (int i = 0; i < el_nr; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    vertex___neigh_v[x].push_back(y + n);
    vertex___neigh_v[y + n].push_back(x);
  }
  vector<int> vertex___is_visited(n + m, 0);
  int components_nr = 0;
  for (int vertex = 0; vertex < n + m; vertex++) {
    if (vertex___is_visited[vertex]) {
      continue;
    }
    stack<int> dfsstack;
    dfsstack.push(vertex);
    vertex___is_visited[vertex] = 1;
    while (!dfsstack.empty()) {
      int curr_v = dfsstack.top();
      dfsstack.pop();
      for (int neigh_v : vertex___neigh_v[curr_v]) {
        if (!vertex___is_visited[neigh_v]) {
          vertex___is_visited[neigh_v] = 1;
          dfsstack.push(neigh_v);
        }
      }
    }
    components_nr++;
  }
  cout << components_nr - 1 << endl;
}
