#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 10;
int N, M, Q;
int P[MAXN];
int find(int here) {
  if (here == P[here]) return here;
  return P[here] = find(P[here]);
}
void merge(int node1, int node2) {
  node1 = find(node1);
  node2 = find(node2);
  if (node1 != node2) P[node1] = node2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> M >> Q;
  for (int i = 1; i <= N + M; i++) P[i] = i;
  for (int i = 0; i < Q; i++) {
    int x, y;
    cin >> x >> y;
    merge(x, N + y);
  }
  int ans = 0;
  for (int i = 1; i <= N + M; i++)
    if (P[i] == i) ans++;
  cout << ans - 1;
  return 0;
}
