#include <bits/stdc++.h>
using namespace std;
int parent[400050];
int getroot(int x) {
  if (parent[x] != x) {
    parent[x] = getroot(parent[x]);
  }
  return parent[x];
}
void Merge(int a, int b) {
  int p1 = getroot(a), p2 = getroot(b);
  if (p1 == p2)
    return;
  else
    parent[p1] = p2;
}
int main() {
  int n, m, q;
  while (scanf("%d%d%d", &n, &m, &q) == 3) {
    for (int i = 1; i <= n + m; i++) parent[i] = i;
    while (q--) {
      int a, b;
      scanf("%d%d", &a, &b);
      Merge(a, b + n);
    }
    int ans = -1;
    for (int i = 1; i <= n + m; i++)
      if (parent[i] == i) ans++;
    cout << ans << endl;
  }
  return 0;
}
