#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 2e9 + 5;
const int maxn = 2e5 + 2;
int n, m, q;
int rowrep[maxn], colrep[maxn], rowds[maxn], colds[maxn];
bool seenrow[maxn] = {0}, seencol[maxn] = {0};
int find(int x, int ds[]) {
  if (ds[x] == x) return x;
  return ds[x] = find(ds[x], ds);
}
int count(int a[], int s) {
  int seen = 0;
  for (int i = 1; i <= s; i++) {
    if (find(i, a) == i) ++seen;
  }
  return seen;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> q;
  memset(rowrep, -1, sizeof rowrep);
  memset(colrep, -1, sizeof colrep);
  for (int i = 1; i < maxn; i++) {
    rowds[i] = colds[i] = i;
  }
  int totcols = 0, totrows = 0;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    if (!seenrow[a]) ++totrows;
    seenrow[a] = 1;
    if (!seencol[b]) ++totcols;
    seencol[b] = 1;
    if (rowrep[a] == -1) {
      rowrep[a] = find(b, colds);
    } else {
      int rep = find(b, colds);
      colds[rep] = find(rowrep[a], colds);
    }
    if (colrep[b] == -1) {
      colrep[b] = find(a, rowds);
    } else {
      int rep = find(a, rowds);
      rowds[a] = find(colrep[b], rowds);
    }
  }
  int r1 = count(rowds, n);
  int r2 = count(colds, m);
  int ans = r1 + m - totcols - 1;
  ans = min(ans, r2 + n - totrows - 1);
  cout << ans;
}
