#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int M = 8000005;
const int N = 200005;
int n, m, k, sum, ans, Min, f[N], fa[N], t1, t2;
pair<int, int> a[N];
int find(int x) { return fa[x] == x ? x : (fa[x] = find(fa[x])); }
void hunt(int x, int y) {
  sort(a + 1, a + 1 + k);
  for (int i = 1; i <= x; i++) fa[i] = i;
  for (int i = 1; i < k; i++)
    if (a[i].first == a[i + 1].first) {
      t1 = find(a[i].second);
      t2 = find(a[i + 1].second);
      if (t1 != t2) fa[t2] = t1;
    }
  sum = 0;
  for (int i = 1; i <= x; i++)
    if (find(i) == i) sum++;
  ans = sum > 1 ? (sum - 1) : 0;
  memset((f), (0), sizeof((f)));
  for (int i = 1; i <= k; i++) f[a[i].first] = 1;
  for (int i = 1; i <= y; i++)
    if (!f[i]) ans++;
  Min = min(ans, Min);
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= k; i++) scanf("%d %d", &a[i].first, &a[i].second);
  Min = INT_MAX;
  hunt(m, n);
  for (int i = 1; i <= k; i++) swap(a[i].first, a[i].second);
  hunt(n, m);
  printf("%d\n", Min);
  return 0;
}
