#include <bits/stdc++.h>
using namespace std;
int n, m, q, x, y, ans;
int fa[400002];
int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); }
void unionn(int x, int y) { fa[find(y)] = find(x); }
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; ++i) fa[i] = i;
  for (int i = 1; i <= q; ++i) {
    cin >> x >> y;
    y += n;
    unionn(x, y);
  }
  for (int i = 1; i <= n + m; ++i) ans += fa[i] == i;
  cout << ans - 1 << endl;
  return 0;
}
