#include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> w[400001];
int check[400001];
int ans;
void input() {
  scanf("%d %d %d", &n, &m, &q);
  int x, y;
  for (int i = (0); i < (q); i++) {
    scanf("%d %d", &y, &x);
    x--;
    y--;
    x += n;
    w[x].push_back(y);
    w[y].push_back(x);
  }
  n = n + m;
}
void dfs(int node) {
  check[node] = 1;
  for (int i = (0); i < (w[node].size()); i++) {
    int next = w[node][i];
    if (check[next] == 0) {
      dfs(next);
    }
  }
}
void process() {
  for (int i = (0); i < (n); i++) {
    if (check[i] == 0) {
      dfs(i);
      ans++;
    }
  }
}
void output() { printf("%d", ans - 1); }
int main() {
  input();
  process();
  output();
  return 0;
}
