#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
struct pi {
  int x;
  int y;
} pp[maxn];
int cmp(pi a, pi b) {
  if (a.x != b.x) return a.x < b.x;
  return a.y < b.y;
}
int x[maxn];
int y[maxn];
int root[maxn];
int find(int p) {
  if (root[p] == p) return root[p];
  return root[p] = find(root[p]);
}
void merg(int a, int b) {
  a = find(a);
  b = find(b);
  root[a] = b;
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &pp[i].x, &pp[i].y);
  }
  for (int i = 1; i <= m; i++) root[i] = i;
  sort(pp, pp + q, cmp);
  for (int i = 0; i < q; i++) {
    int p = i;
    while (p < q && pp[i].x == pp[p].x) {
      p++;
    }
    x[pp[i].x] = 1;
    for (int j = i; j < p; j++) {
      merg(pp[i].y, pp[j].y);
    }
    if (pp[i].x == n) {
      for (int j = i; j < p; j++) {
        y[find(pp[j].y)] = 1;
      }
    }
    i = p - 1;
  }
  int s = 0;
  for (int i = 1; i <= m; i++) {
    if (find(i) == i && y[find(i)] == 0) {
      s++;
    }
  }
  for (int i = 1; i < n; i++) {
    if (!x[i]) {
      s++;
    }
  }
  cout << s << endl;
}
