#include <bits/stdc++.h>
using namespace std;
int f[410000];
int vis[410000];
int sum = 0;
int finds(int x) { return x == f[x] ? x : f[x] = finds(f[x]); }
void unions(int x, int y) {
  x = finds(x);
  y = finds(y);
  if (x != y) f[x] = y;
  return;
}
int main() {
  int n, m, q;
  int x, y;
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; i++) {
    f[i] = i;
  }
  while (q--) {
    cin >> x >> y;
    unions(x, y + n);
  }
  for (int i = 1; i <= n + m; i++) {
    vis[finds(i)]++;
    if (vis[f[i]] == 1) sum++;
  }
  cout << sum - 1 << endl;
  return 0;
}
