# include <stdio.h>
int pre[101];
struct node
{
int x, y;
}a[101];
void init(int n)
{
for(int i=0; i<=n; ++i)
pre[i] = i;
}
int find(int x)
{
if(x != pre[x])
pre[x] = find(pre[x]);
return pre[x];
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init(n);
int ans = 0;
for(int i=0; i<n; ++i)
scanf("%d%d",&a[i].x, &a[i].y);
for(int i=0; i<n; ++i)
for(int j=i+1; j<n; ++j)
{
if(a[i].x==a[j].x || a[i].y == a[j].y)
{
int px = find(i);
int py = find(j);
if(px != py)
{
++ans;
pre[py] = px;
}
}
}
printf("%d\n",n-1-ans);
}
return 0;
}