#include <bits/stdc++.h>
using namespace std;
int fa[400005];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 1; i <= m + n; i++) fa[i] = i;
  int ans = m + n - 1;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    b += n;
    if (Find(a) != Find(b)) {
      fa[Find(a)] = fa[b];
      ans--;
    }
  }
  cout << ans << endl;
  return 0;
}
