#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int INF = 1e9 + 7;
int n, m, q, x, y, ans;
int Root[maxn], Rank[maxn];
void Init() {
  for (int i = 0; i <= n + m; i++) Root[i] = i;
}
int Find(int x) {
  if (Root[x] == x)
    return x;
  else
    return Root[x] = Find(Root[x]);
}
void Unite(int x, int y) {
  int root_x = Find(x);
  int root_y = Find(y);
  if (root_x == root_y) return;
  if (Rank[root_x] < Rank[root_y]) {
    Root[root_x] = Root[root_y];
  } else if (Rank[root_x] > Rank[root_y]) {
    Root[root_y] = Root[root_x];
  } else {
    Root[root_y] = Root[root_x];
    Rank[root_x]++;
  }
}
int main() {
  while (cin >> n >> m >> q) {
    Init();
    for (int i = 0; i < q; i++) {
      cin >> x >> y;
      Unite(x, y + n);
    }
    ans = -1;
    for (int i = 1; i <= n + m; i++)
      if (Find(i) == i) ans++;
    cout << ans << endl;
  }
  return 0;
}
