#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int tt;
int ss = n + m;
int fa[400005];
bool vis[500005];
int ans;
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) {
    fa[i] = i;
  }
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    fa[find(x)] = find(n + y);
  }
  for (int i = 1; i <= n + m; i++) {
    if (!vis[find(i)]) {
      vis[fa[i]] = 1;
      ans++;
    }
  }
  cout << ans - 1;
  return 0;
}
