N = int(input())

xmap = {}
ymap = {}


comps = 0
for i in range(N):
    (x,y) = input().split(" ")
    x = int(x)
    y = int(y)
    
    if not (x in xmap or y in ymap):
        comps += 1
    xmap[x] = True
    ymap[y] = True

print(comps - 1)