#include <bits/stdc++.h>
using namespace std;
int f[500005];
int fa(int x) { return f[x] = (f[x] == x ? x : fa(f[x])); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; ++i) f[i] = i;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    x = fa(x);
    y = fa(y + n);
    if (x != y) f[y] = x;
  }
  int cnt = 0;
  for (int i = 1; i <= n + m; ++i) {
    cnt += (fa(i) == i);
  }
  cout << cnt - 1 << endl;
}
