#include <bits/stdc++.h>
using namespace std;
vector<int> p(400400);
int find(int u) { return (u == p[u] ? u : p[u] = find(p[u])); }
bool merge(int u, int v) {
  u = find(u), v = find(v);
  if (u == v) {
    return false;
  }
  p[v] = u;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  iota(p.begin(), p.end(), 0);
  int n, m, q;
  cin >> n >> m >> q;
  int res = n + m - 1;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    y += n;
    res -= merge(x, y);
  }
  cout << res << '\n';
}
