#include <bits/stdc++.h>
using namespace std;
int fa[400010];
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m, q, ans = -1;
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; i++) fa[i] = i;
  for (int i = 1; i <= q; i++) fa[find(gi())] = find(gi() + n);
  for (int i = 1; i <= n + m; i++) ans += (fa[i] == i);
  cout << ans;
  return 0;
}
