#include<cstdio>usingnamespace std;int f[400010];intget(int x){if(f[x]== x)return x;return f[x]=get(f[x]);}intmain(){int n, m, q, i, x, y;scanf("%d%d%d",&n,&m,&q);for(i =1; i <= n + m; i++) f[i]= i;for(i =1; i <= q; i++){scanf("%d%d",&x,&y);
x =get(x), y =get(y + n);
f[x]= y;}int s =0;for(i =1; i <= n + m; i++)if(get(i)== i) s++;printf("%d", s -1);return0;}