#include <bits/stdc++.h>
using namespace std;
inline int Read() {
  int res = 0, f = 1;
  char c;
  while (c = getchar(), c < 48 || c > 57)
    if (c == '-') f = 0;
  do res = (res << 3) + (res << 1) + (c ^ 48);
  while (c = getchar(), c >= 48 && c <= 57);
  return f ? res : -res;
}
template <class T>
inline bool Min(T &a, T const &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool Max(T &a, T const &b) {
  return a < b ? a = b, 1 : 0;
}
const int N = 2e5 + 5;
int q, n, m, x, y, Ans, F[N << 1];
inline int find(int x) { return F[x] == x ? x : F[x] = find(F[x]); }
int main() {
  n = Read(), m = Read(), q = Read();
  for (register int i = (1), i_end_ = (n + m); i <= i_end_; ++i) F[i] = i;
  for (register int i = (1), i_end_ = (q); i <= i_end_; ++i) {
    int x = Read(), y = Read();
    F[find(y + n)] = find(x);
  }
  for (register int i = (1), i_end_ = (n + m); i <= i_end_; ++i)
    if (find(i) == i) Ans++;
  printf("%d", Ans - 1);
}
