#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
const long long inf = numeric_limits<long long>::max() / 2;
const long double eps = 1e-9;
const long double pi = acos(-1);
struct graph {
  long long n;
  vector<vector<long long> > adj;
  graph() {}
  graph(long long n_) {
    n = n_;
    adj.resize(n);
  }
  void insert(long long v, long long u) {
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
};
long long n, m, q;
graph g;
vector<long long> used;
void dfs(long long v) {
  used[v] = 1;
  for (long long u : g.adj[v]) {
    if (used[u]) {
      continue;
    }
    dfs(u);
  }
}
inline void solve() {
  used.resize(g.n, 0);
  long long ans = 0;
  for (long long i = 0; i < g.n; i++) {
    if (!used[i]) {
      ++ans;
      dfs(i);
    }
  }
  --ans;
  cout << ans << "\n";
}
inline void read() {
  cin >> n >> m >> q;
  g = graph(n + m);
  for (long long i = 0; i < q; i++) {
    long long r, c;
    cin >> r >> c;
    --r, --c;
    g.insert(r, n + c);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  read();
  solve();
  return 0;
}
