#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
inline void read(int &x) {
  int f = 1;
  x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  x *= f;
}
inline void print(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) print(x / 10);
  putchar(x % 10 + '0');
}
inline void caltime(int tt) {
  tt = clock() - tt;
  cerr << (double)tt / CLOCKS_PER_SEC << " seconds!" << endl;
}
int n, m, q;
vector<int> e[maxn * 10];
bool vis[maxn * 10];
void dfs(int x) {
  vis[x] = 1;
  for (int i = 0; i < e[x].size(); i++) {
    if (!vis[e[x][i]]) dfs(e[x][i]);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    read(x), read(y);
    e[x].push_back(y + n);
    e[y + n].push_back(x);
  }
  int ans = 0;
  for (int i = 1; i <= m + n; i++) {
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
