#include <bits/stdc++.h>
using namespace std;
template <class T>
using lim = numeric_limits<T>;
const int maxn = 2e5;
int table[maxn * 2 + 2];
int find(int i) {
  if (table[i] == i)
    return i;
  else
    return table[i] = find(table[i]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 2 * maxn + 2; i++) table[i] = i;
  int n, m, q;
  cin >> n >> m >> q;
  int ans = n + m - 1;
  while (q--) {
    int x, y;
    cin >> x >> y;
    x = find(x);
    y = find(y + n);
    if (x == y) continue;
    ans--;
    table[x] = y;
  }
  cout << ans << "\n";
}
