#include <bits/stdc++.h>
using namespace std;
const int N = 400010;
long long m, n, num[N], ans, fa[N];
bool vis[N];
long long find(long long u) { return u == fa[u] ? u : fa[u] = find(fa[u]); }
int main() {
  long long t, u, v;
  cin >> n >> m >> t;
  for (int i = 1; i <= m + n; i++) fa[i] = i;
  for (int i = 1; i <= t; i++) {
    scanf("%lld%lld", &u, &v);
    fa[find(u)] = fa[find(v + n)];
  }
  for (int i = 1; i <= m + n; i++) {
    if (!vis[find(i)]) {
      ans++;
      vis[find(i)] = 1;
    }
  }
  cout << ans - 1;
}
