#include <bits/stdc++.h>
using namespace std;
const int N = 10000100;
int n, m, k;
int h[N], e[N], ne[N], idx;
bool st[N];
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
void dfs(int u) {
  st[u] = true;
  for (int i = h[u]; i != -1; i = ne[i]) {
    int j = e[i];
    if (st[j]) {
      continue;
    }
    dfs(j);
  }
}
int main() {
  int cnt = 0;
  cin >> n >> m >> k;
  memset(h, -1, sizeof h);
  while (k--) {
    int a, b;
    cin >> a >> b;
    add(a, b + n), add(b + n, a);
  }
  for (int i = 1; i <= n + m; i++) {
    if (!st[i]) {
      dfs(i);
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}
