#include <bits/stdc++.h>
#pragma optimize("-O3")
using namespace std;
const int N = 400005;
int n, m, q;
vector<int> G[N];
bool vis[N];
inline void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i];
    if (!vis[v]) dfs(v);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin.tie(nullptr);
  cin >> n >> m >> q;
  for (int i = 1; i <= q; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v + n), G[v + n].push_back(u);
  }
  int k = n + m;
  int ans = -1;
  for (int i = 1; i <= k; ++i) {
    if (!vis[i]) {
      dfs(i);
      ++ans;
    }
  }
  return !printf("%d\n", ans);
}
