/*
题意:给出n个点的坐标,如果两个点x相同或者y相同,则两点可以联通,
问你最少加几条线,能使全部点联通
类型:并查集
分析:属于裸并查集了吧..
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n;
int fa[1005];
struct Node{
int x,y;
};
Node a[1005];
int Find(int x){
if(fa[x]==x)return x;
return fa[x]=Find(fa[x]);
}
void unite(int x,int y){
x=Find(x);
y=Find(y);
if(x!=y)fa[x]=y;
}
int main()
{
while(~scanf("%d",&n)){
for(int i=0;i<=n;i++)fa[i]=i;
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=0;j<n;j++){
if(a[j].x==a[i].x||a[j].y==a[i].y){
unite(i,j);
}
}
int res=0;
for(int i=0;i<n;i++){
if(fa[i]==i)res++;
}
printf("%d\n",res-1);
}
return 0;
}