#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
inline long long power(long long a, long long b) {
  long long x = 1;
  a = a % 1000000007ULL;
  while (b) {
    if (b & 1) x = (x * a) % 1000000007ULL;
    a = (a * a) % 1000000007ULL;
    b >>= 1;
  }
  return x;
}
inline long long inv(long long a) { return power(a, 1000000007ULL - 2); }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long n, m, q;
map<pair<int, int>, bool> check;
int p[400005], rnk[400005];
set<int> ss;
int find(int v) { return v == p[v] ? v : p[v] = find(p[v]); }
void merge(int u, int v) { p[find(u)] = find(v); }
int main() {
  cin >> n >> m >> q;
  int x, y;
  int ans = 0;
  for (int i = 1; i <= 400000; i++) {
    p[i] = i;
    rnk[i] = 0;
  }
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    merge(x, y + 200000);
  }
  for (int i = 1; i <= n; i++) ss.insert(find(i));
  for (int i = 200001; i <= 200000 + m; i++) ss.insert(find(i));
  ans = ss.size() - 1;
  cout << ans << endl;
  return 0;
}
