#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
vector<long long> sz(200005), p(200005);
long long find(long long x) { return x == p[x] ? p[x] : (p[x] = find(p[x])); }
void merge(long long x, long long y) {
  x = find(x), y = find(y);
  if (sz[x] < sz[y]) swap(x, y);
  sz[x] += sz[y];
  p[y] = x;
  sz[y] = 0;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m, q;
  cin >> n >> m >> q;
  for (long long i = 1; i <= max(n, m); i++) {
    p[i] = i;
    sz[i] = 1;
  }
  vector<long long> g[m + 1];
  for (long long i = 0; i < q; i++) {
    long long x, y;
    cin >> x >> y;
    g[y].push_back(x);
  }
  long long ans = 0;
  for (long long i = 1; i <= m; i++) {
    if (g[i].size() == 0) ans++;
    for (long long j = 1; j < g[i].size(); j++) {
      merge(g[i][j - 1], g[i][j]);
    }
  }
  for (long long i = 1; i <= n; i++) {
    ans += p[i] == i;
  }
  cout << ans - 1 << "\n";
}
