#include <bits/stdc++.h>
using namespace std;
vector<int> p;
int find(int n) {
  if (p[n] < 0) return n;
  return p[n] = find(p[n]);
}
bool merge(int a, int b) {
  a = find(a);
  b = find(b);
  if (a != b) {
    p[a] = b;
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, ans = 0, q;
  cin >> n >> m >> q;
  p.assign(n + m + 1, -1);
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    merge(a, n + b);
  }
  for (int i = 1; i < n + m; i++) {
    ans += merge(i, i + 1);
  }
  cout << ans;
}
