#include <bits/stdc++.h>
inline void read(int& x) {
  char c;
  while ((c = getchar()) < '0' || c > '9')
    ;
  x = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
}
using namespace std;
const int maxn = 4e5 + 10;
const int M = 128;
const int inf = 0x7fffffff;
const long long INF = 9E18;
const int mod = 1e9 + 7;
const int eps = 1e-7;
const double pi = 3.1415926535898;
int fa[maxn];
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n + m; i++) fa[i] = i;
  int ans = m + n - 1;
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    y += n;
    int xx = getfa(x);
    int yy = getfa(y);
    if (xx != yy) {
      fa[xx] = yy;
      ans--;
    }
  }
  cout << ans << endl;
}
