#include <bits/stdc++.h>
using namespace std;
vector<int> parent;
vector<int> Rank;
int root(int v) {
  if (parent[v] == v) return v;
  return parent[v] = root(parent[v]);
}
void DSU(int a, int b) {
  a = root(a);
  b = root(b);
  if (a != b) {
    if (Rank[b] > Rank[a]) swap(a, b);
    Rank[a] += Rank[b];
    parent[b] = a;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, q;
  cin >> n >> m >> q;
  parent.resize(n + m + 3);
  for (int i = 0; i < parent.size(); i++) {
    parent[i] = i;
  }
  Rank.resize(n + m + 3, 1);
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    b += n;
    DSU(a, b);
  }
  set<int> roots;
  for (int i = 1; i <= n + m; i++) {
    roots.insert(root(i));
  }
  cout << roots.size() - 1 << endl;
  return 0;
}
