import queue

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

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

q = []

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)

heads = int(input())
for i in range(heads):
    x,y = [int(z) for z 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

    for newNode in cols[node.x]:
        if newNode.visited:
            continue
        q.append(newNode)
    for newNode in rows[node.y]:
        if newNode.visited:
            continue
        q.append(newNode)
    while len(q) > 0:
        myNode = q.pop(0)
        myNode.visited = True
        for newNode in cols[myNode.x]:
            if newNode.visited:
                continue
            q.append(newNode)
        for newNode in rows[myNode.y]:
            if newNode.visited:
                continue
            q.append(newNode)

print(counter)

