#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 200019;
int p[N + N + 1];
void init() {
  for (int i = 0; i < N + N + 1; i++) {
    p[i] = i;
  }
}
int fin(int a) {
  if (a == p[a]) return a;
  return p[a] = fin(p[a]);
}
bool merge(int a, int b) {
  a = fin(a);
  b = fin(b);
  if (a == b) return false;
  p[b] = a;
  return true;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  init();
  int n, m, q;
  cin >> n >> m >> q;
  int el = n + m - 1;
  int x, y;
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    el -= merge(x, y + N);
  }
  cout << max(0, el);
  return 0;
}
