#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int SZ = 2e5 + 100;
int p[SZ], r[SZ];
vector<int> c[SZ];
int get(int v) {
  if (v != p[v]) p[v] = get(p[v]);
  return p[v];
}
void join(int u, int v) {
  v = get(v);
  u = get(u);
  if (r[v] < r[u]) swap(v, u);
  p[u] = v;
  if (r[u] = r[v]) r[v]++;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  int cnt = 0;
  int comp = n;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    if (c[y].empty()) cnt++;
    c[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j < c[i].size(); j++)
      if (get(c[i][0]) != get(c[i][j])) {
        join(c[i][0], c[i][j]);
        comp--;
      }
  }
  cout << comp - 1 + m - cnt;
  return 0;
}
