#include <bits/stdc++.h>
using namespace std;
const long long int inf = 9e18;
void __print(long long int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  long long int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
vector<vector<long long int>> adj;
const long long int N = 4e5 + 1;
vector<long long int> vis(N + 1, 0);
void dfs(long long int src) {
  vis[src] = 1;
  for (auto nbr : adj[src]) {
    if (!vis[nbr]) {
      dfs(nbr);
    }
  }
}
void tc() {
  long long int n, m, q;
  cin >> n >> m >> q;
  adj.reserve(N);
  while (q--) {
    long long int r, c;
    cin >> r >> c;
    long long int x = r;
    long long int y = n + c;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  long long int res = 0;
  for (long long int i = 1; 1 < n + m + 1 ? i < n + m + 1 : i > n + m + 1;
       1 < n + m + 1 ? i += 1 : i -= 1) {
    if (!vis[i]) {
      dfs(i);
      res++;
    }
  }
  cout << res - 1 << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  while (t--) {
    tc();
  }
}
