from queue import Queue


def parse_input():
    n = int(input())
    heads = []
    for _ in range(n):
        heads.append(list(map(int, input().split())))
    return n, heads


def binary_search(heads, value, key):
    N = len(heads)
    n = N // 2
    offset = 0
    while key(heads[offset + n]) != value and n:
        if key(heads[offset + n]) < value:
            offset = offset + n
        n //= 2

    n = offset + n
    if key(heads[n]) == value:
        result = [heads[n]]
        idx = n - 1
        while idx > -1 and key(heads[idx]) == value:
            result.append(heads[idx])
            idx -= 1
        idx = n + 1
        while idx < N and key(heads[idx]) == value:
            result.append(heads[idx])
            idx += 1
        return result
    return []


def calculate_different_groups(n, heads):
    x_sorted = sorted(heads, key=lambda head: head[0])
    y_sorted = sorted(heads, key=lambda head: head[1])
    N = len(heads)
    in_group = [False] * N
    count = 0
    for i, (hx, hy, _) in enumerate(heads):
        if not in_group[i]:
            count += 1
            in_group[i] = True
            x_vals = set()
            y_vals = set()
            q = Queue()
            q.put([None, hy])
            q.put([hx, None])
            while not q.empty():
                x, y = q.get()
                if x is not None and x not in x_vals:
                    res = binary_search(x_sorted, x, lambda head: head[0])
                    for h in res:
                        q.put([None, h[1]])
                        in_group[h[2]] = True
                    x_vals.add(x)
                if y is not None and y not in y_vals:
                    res = binary_search(y_sorted, y, lambda head: head[1])
                    for h in res:
                        q.put([h[0], None])
                        in_group[h[2]] = True
                    y_vals.add(y)
    return count

def main():
    n, heads = parse_input()
    heads = [(head[0], head[1], i) for i, head in enumerate(heads)]
    answer = calculate_different_groups(n, heads)
    print(answer)

if __name__ == "__main__":
    main()
