class Node:
    def __init__(self, _x, _y):
        self.x = _x
        self.y = _y
        self.visited = False

cols = dict()
rows = dict()
nodes = []

def f(node: Node):
    node.visited = True
    for newNode in cols[node.x]:
        if newNode.visited:
            continue
        f(newNode)
    for newNode in rows[node.y]:
        if newNode.visited:
            continue
        f(newNode)

for i in range(int(input())):
    x,y = [int(x) for x in input().split()]
    node = Node(x, y)
    if x not in cols:
        cols[x] = []
    cols[x].append(node)
    if y not in rows:
        rows[y] = []
    rows[y].append(node)
    nodes.append(node)

counter = -1
for node in nodes:
    if node.visited:
        continue
    counter += 1
    node.visited = True
    f(node)

print(counter)

