#include <bits/stdc++.h>
using namespace std;
template <class K>
inline bool cmax(K& a, const K& b) {
  return (a < b) ? a = b, 1 : 0;
}
template <class K>
inline bool cmin(K& a, const K& b) {
  return (a > b) ? a = b, 1 : 0;
}
inline int read() {
  register int s = 0;
  register char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) s = (s << 1) + (s << 3) + c - '0', c = getchar();
  return s;
}
int n, m, p, f[400007], res;
inline int get(register int x) { return (f[x] != x) ? f[x] = get(f[x]) : x; }
int main() {
  register int i, x, y;
  n = read(), m = read(), p = read();
  for (i = 1; i <= n + m; i++) f[i] = i;
  for (i = 1; i <= p; i++) {
    x = get(read()), y = get(read() + n);
    if (x != y) {
      f[y] = x, ++res;
    }
  }
  printf("%d", n + m - res - 1);
  return 0;
}
