#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long INF = 1LL << 60;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct UnionFind {
  vector<long long> par, w;
  UnionFind(long long n) : par(n, -1), w(n, 0) {}
  void init(long long n) {
    par.assign(n, -1);
    w.assign(n, 0);
  }
  long long root(long long x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }
  bool issame(long long x, long long y) { return root(x) == root(y); }
  bool merge(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      ++w[x];
      return false;
    }
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    w[x] += w[y];
    ++w[x];
    return true;
  }
  long long size(long long x) { return -par[root(x)]; }
  long long wei(long long x) { return w[root(x)]; }
};
void solve() {
  long long n, m, q;
  cin >> n >> m >> q;
  UnionFind uf(n + m);
  long long cnt = n + m;
  for (long long i = 0; i < q; ++i) {
    long long r, c;
    cin >> r >> c;
    --r, --c;
    if (!uf.issame(r, c + n)) {
      uf.merge(r, c + n);
      --cnt;
    }
  }
  cout << cnt - 1 << "\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
