#include <bits/stdc++.h>
int main() {
  int i;
  int a[200001] = {0};
  int d[200001] = {0};
  for (i = 1; i <= 200000; i++) {
    a[i] = i;
  }
  int n, m, q;
  while (scanf("%d%d%d", &n, &m, &q) > 0) {
    int line = n;
    int head;
    int x, y;
    for (i = q; i > 0; i--) {
      scanf("%d%d", &x, &y);
      if (d[x] != y) {
        if (d[x] == 0) {
          d[x] = y;
          head = d[x];
          while (a[head] != head) {
            head = a[head];
          }
          while (a[d[x]] != d[x]) {
            a[d[x]] = head;
            d[x] = a[d[x]];
          }
        } else {
          head = y;
          while (a[head] != head) {
            head = a[head];
          }
          while (a[y] != y) {
            a[y] = head;
            y = a[y];
          }
          while (a[d[x]] != d[x]) {
            d[x] = a[d[x]];
          }
          while (a[d[x]] != d[x]) {
            a[d[x]] = head;
            d[x] = a[d[x]];
          }
          a[y] = d[x];
        }
      }
    }
    int same = 0;
    for (i = 1; i <= m; i++) {
      if (a[i] == i) {
        same++;
      }
    }
    for (i = 1; i <= n; i++) {
      if (d[i] != 0) {
        line--;
      }
    }
    printf("%d\n", same + line - 1);
  }
}
