human
Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.

We assume that Bajtek can only heap up snow drifts at integer coordinates.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.

Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.

Output

Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

Examples

Input

2
2 1
1 2

Output

1

Input

2
2 1
4 1

Output

0
Solve the task in PYTHON3.

Can you provide a Python code to find the minimal number of snow drifts that need to be created?
assistant
#Here is Python code to find the minimal number of snow drifts that need to be created.

import math
class disjoint:
    def __init__(self,n):
        self.rank=[1]*n
        self.parent=[i for i in range(n)]
        
        
       
    def find(self,x):
        if(self.parent[x]!=x):
            self.parent[x]=self.find(self.parent[x])
        return self.parent[x];
       
    def union(self,x,y):
        xid=self.find(x)
        yid=self.find(y)
        
        if(xid==yid):
            return;
        if(self.rank[xid]<self.rank[yid]):
            self.parent[xid]=yid
            
                  
            return;
        elif(self.rank[xid]>self.rank[yid]):
            self.parent[yid]=xid # merging y into x
           
            return;
        else:
            self.parent[yid]=xid
            self.rank[xid]+=1
          
            return;        
        

ans=[]
n=int(input())
obj=disjoint(n)
for i in range(n):
    x,y=map(int,input().split())
    ans.append([x,y])
for i in range(len(ans)):
    for j in range(i+1,len(ans)):
        t=ans[i]
        m=ans[j]
        if(t[0]==m[0] or t[1]==m[1]):
            obj.union(i,j)
from collections import *
c1=0
al=defaultdict(int)
for i in range(len(ans)):
    if(al[obj.find(i)]==0):
        al[obj.find(i)]=1
        c1+=1
print(max(c1-1,0))
    
        
    
    

        
    

    

#init function sort of creates the parent and the rank list
#find function is used for to find rank
#union function is used for doing the union
#what does path function do , it makes find operation almost linear
#makes our tree of very less height

#use of log *n function -

#number times log has to be applied to make log value as 1

#by using path compression time complexty of each function is log*n

#