#include <bits/stdc++.h>
using namespace std;
namespace FastIO {
template <typename tp>
inline void read(tp &x) {
  x = 0;
  register char c = getchar();
  register bool f = 0;
  for (; c < '0' || c > '9'; f |= (c == '-'), c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
    ;
  if (f) x = -x;
}
template <typename tp>
inline void write(tp x) {
  if (x == 0) return (void)(putchar('0'));
  if (x < 0) putchar('-'), x = -x;
  int pr[20];
  register int cnt = 0;
  for (; x; x /= 10) pr[++cnt] = x % 10;
  while (cnt) putchar(pr[cnt--] + '0');
}
template <typename tp>
inline void writeln(tp x) {
  write(x);
  putchar('\n');
}
}  // namespace FastIO
using namespace FastIO;
int p[400008];
int n, m, k, ans;
int find(int a) { return a == p[a] ? a : p[a] = find(p[a]); }
int main() {
  for (int i = 1; i <= 400005; ++i) p[i] = i;
  read(n), read(m), read(k);
  ans = n + m - 1;
  while (k--) {
    int x, y;
    read(x), read(y);
    y += n;
    x = find(x), y = find(y);
    if (x != y) {
      p[x] = y, ans--;
    }
  }
  writeln(ans);
  return 0;
}
