#include <bits/stdc++.h>
using namespace std;
const int MAX = 400005;
int ans = 0, n, m, q;
int fa[MAX];
int get(int x) {
  if (fa[x] == x) return x;
  return fa[x] = get(fa[x]);
}
void merge(int x, int y) { fa[get(x)] = get(y); }
bool query(int x, int y) { return get(x) == get(y); }
void Search() {
  for (int i = 1; i <= n + m; i++) {
    if (fa[i] == i) {
      ans++;
    }
  }
}
int main() {
  int x, y;
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; i++) {
    fa[i] = i;
  }
  for (int i = 1; i <= q; i++) {
    cin >> x >> y;
    merge(x, y + n);
  }
  Search();
  cout << ans - 1 << endl;
  return 0;
}
