#include <bits/stdc++.h>
using namespace std;
int n, m, s, ans = 0, f[2 * 200005];
inline int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  scanf("%d%d%d", &n, &m, &s);
  for (register int i = 1; i <= m + n; i++) f[i] = i;
  for (register int i = 1; i <= s; i++) {
    register int a, b;
    scanf("%d%d", &a, &b), b += n;
    f[find(b)] = find(a);
  }
  for (register int i = 1; i <= n + m; i++)
    if (f[i] == i) ans++;
  printf("%d", ans - 1);
  return 0;
}
