#include <bits/stdc++.h>
using namespace std;
int pai[1000010];
int find(int x) {
  if (pai[x] == x) return x;
  return pai[x] = find(pai[x]);
}
void join(int a, int b) { pai[find(a)] = find(b); }
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; i++) pai[i] = i;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    b += n;
    join(a, b);
  }
  int comps = 0;
  for (int i = 1; i <= n + m; i++) comps += pai[i] == i;
  cout << comps - 1 << endl;
  return 0;
}
