#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 200005;
const int MOD = 1e9 + 9;
vector<int> g[MAXN];
int n, m, q, ans;
int rows[MAXN];
bool vist[MAXN];
pair<int, int> samp[MAXN];
void dfs(int st) {
  if (vist[st]) return;
  vist[st] = 1;
  for (int i = 0; i < g[st].size(); i++) {
    dfs(g[st][i]);
  }
}
void buildG() {
  sort(samp, samp + q);
  for (int i = 1; i < q; ++i) {
    int r = samp[i].first, c = samp[i].second;
    int rl = samp[i - 1].first, cl = samp[i - 1].second;
    if (r == rl) {
      g[c].push_back(cl);
      g[cl].push_back(c);
    }
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 0; i < q; ++i) {
    int r, c;
    scanf("%d %d", &r, &c);
    r--;
    c--;
    rows[r] = 1;
    samp[i] = {r, c};
  }
  buildG();
  for (int i = 0; i < m; ++i) {
    if (!vist[i]) {
      ans++;
      dfs(i);
    }
  }
  ans--;
  for (int i = 1; i < n; ++i) rows[i] += rows[i - 1];
  ans += n - rows[n - 1];
  printf("%d\n", ans);
  return 0;
}
