#include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> a[400002];
bool vis[400002];
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i < a[u].size(); i++) {
    int v = a[u][i];
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    int x, y;
    cin >> x >> y;
    y += n;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  int cnt = 0;
  for (int i = 1; i <= n + m; i++)
    if (!vis[i]) {
      dfs(i);
      cnt++;
    }
  cout << cnt - 1;
}
