#include <bits/stdc++.h>
int Fa[200010];
std::vector<int> v[200010];
int Find(int x) { return Fa[x] == x ? x : Fa[x] = Find(Fa[x]); }
bool vis[200010];
inline void merge(int a, int b) {
  int f1 = Find(a), f2 = Find(b);
  if (f1 != f2) {
    Fa[f1] = f2;
  }
}
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (; q; --q) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
  }
  for (int i = 1; i <= m; ++i) Fa[i] = i;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (!v[i].size())
      ++ans;
    else {
      int a = v[i][0];
      for (int j = 1, _ = v[i].size(); j < _; ++j) merge(a, v[i][j]);
    }
  }
  for (int i = 1; i <= m; ++i)
    if (!vis[Find(i)]) ++ans, vis[Find(i)] = 1;
  printf("%d\n", ans - 1);
  return 0;
}
