#include <bits/stdc++.h>
using namespace std;
int n, m, q, f[400050];
int F(int x) { return f[x] == x ? x : f[x] = F(f[x]); }
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) f[i] = i;
  for (int i = 1, x, y; i <= q; i++) {
    scanf("%d%d", &x, &y);
    y += n;
    x = F(x), y = F(y);
    if (x != y) f[x] = y;
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++)
    if (f[i] == i) ans++;
  printf("%d\n", ans - 1);
}
