def line_ints(): return list(map(int, input().split()))


class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size))
        self.rank = [0] * size
    
    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):
        rootx = self.find(x)
        rooty = self.find(y)

        if rootx == rooty:
            return False
        
        if self.rank[rootx] < self.rank[rooty]:
            self.parent[rootx] = rooty
        elif self.rank[rooty] < self.rank[rootx]:
            self.parent[rooty] = rootx
        else:
            self.parent[rooty] = rootx
            self.rank[rootx] += 1

        return True
    
n = int(input())

coords = []
for i in range(n):
    x, y = line_ints()
    coords.append((x, y, i))

uf = UnionFind(len(coords))
coords.sort()
for i in range(1, len(coords)):
    if coords[i-1][0] == coords[i][0]:
        uf.union(coords[i-1][2], coords[i][2])

coords.sort(key=lambda x: x[1])
for i in range(1, len(coords)):
    if coords[i-1][1] == coords[i][1]:
        uf.union(coords[i-1][2], coords[i][2])

for i in range(len(coords)):
    uf.find(i)

print(len(set(uf.parent)) - 1)