#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 6;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
int n, m, q;
vector<int> v[maxn];
int vis[maxn];
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  for (int y : v[x]) {
    dfs(y);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    y += n;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int ans = 0;
  for (int i = 1; i <= n + m; i++) {
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  }
  printf("%d\n", ans - 1);
}
