n = int(eval(input()))

xy = [list(map(int, input().split())) for _ in range(n)]

xtoy = {}

ytox = {}

for x, y in xy:

    if x not in xtoy:

        xtoy[x] = []

    xtoy[x].append(y)

    if y not in ytox:

        ytox[y] = []

    ytox[y].append(x)

ret = 0

while len(xtoy) > 0:

    x = next(iter(xtoy))

    xs = set()

    ys = set()

    xs.add(x)

    q = [(x, None)]

    while len(q) > 0:

        (xx, yy) = q.pop(0)

        if xx is not None:

            for y in xtoy.pop(xx):

                if y not in ys:

                    ys.add(y)

                    q.append((None, y))

        if yy is not None:

            for x in ytox.pop(yy):

                if x not in xs:

                    xs.add(x)

                    q.append((x, None))

    ret += len(xs) * len(ys)

print((ret - n))

import sys

sys.setrecursionlimit(100000)



n = int(eval(input()))

xy = [list(map(int, input().split())) for _ in range(n)]

xtoy = {}

ytox = {}

for x, y in xy:

    if x not in xtoy:

        xtoy[x] = [y]

    else:

        xtoy[x].append(y)

    if y not in ytox:

        ytox[y] = [x]

    else:

        ytox[y].append(x)



def doit(x, xs, ys, xtoy, ytox):

    ret = 0

    if x in xtoy:

        xs.add(x)

        yl = xtoy.pop(x)

        ret += len(yl)

        for y in yl:

            ret += doit(y, ys, xs, ytox, xtoy)

    return ret



ret = 0

while len(xtoy) > 0:

    x = next(iter(xtoy))

    xs = set()

    ys = set()

    num = doit(x, xs, ys, xtoy, ytox)

    ret += len(xs) * len(ys) - num // 2



print(ret)
n = int(eval(input()))
xy = [list(map(int, input().split())) for _ in range(n)]
xtoy = {}
ytox = {}
for x, y in xy:
    if x not in xtoy:
        xtoy[x] = []
    xtoy[x].append(y)
    if y not in ytox:
        ytox[y] = []
    ytox[y].append(x)
ret = 0
while len(xtoy) > 0:
    x = next(iter(xtoy))
    xs = set()
    ys = set()
    xs.add(x)
    q = [(x, None)]
    while len(q) > 0:
        (xx, yy) = q.pop(0)
        if xx is not None:
            for y in xtoy.pop(xx):
                if y not in ys:
                    ys.add(y)
                    q.append((None, y))
        if yy is not None:
            for x in ytox.pop(yy):
                if x not in xs:
                    xs.add(x)
                    q.append((x, None))
    ret += len(xs) * len(ys)
print((ret - n))

import sys

sys.setrecursionlimit(100000)
n = int(eval(input()))
xy = [list(map(int, input().split())) for _ in range(n)]
xtoy = {}
ytox = {}
for x, y in xy:
    if x not in xtoy:
        xtoy[x] = [y]
    else:
        xtoy[x].append(y)
    if y not in ytox:
        ytox[y] = [x]
    else:
        ytox[y].append(x)


def doit(x, xs, ys, xtoy, ytox):
    ret = 0
    if x in xtoy:
        xs.add(x)
        yl = xtoy.pop(x)
        ret += len(yl)
        for y in yl:
            ret += doit(y, ys, xs, ytox, xtoy)
    return ret


ret = 0
while len(xtoy) > 0:
    x = next(iter(xtoy))
    xs = set()
    ys = set()
    num = doit(x, xs, ys, xtoy, ytox)
    ret += len(xs) * len(ys) - num // 2
print(ret)

+import sys + +sys.setrecursionlimit(100000) -        xtoy[x] = [] -    xtoy[x].append(y) +        xtoy[x] = [y] +    else: +        xtoy[x].append(y) -        ytox[y] = [] -    ytox[y].append(x) +        ytox[y] = [x] +    else: +        ytox[y].append(x) + + +def doit(x, xs, ys, xtoy, ytox): +    ret = 0 +    if x in xtoy: +        xs.add(x) +        yl = xtoy.pop(x) +        ret += len(yl) +        for y in yl: +            ret += doit(y, ys, xs, ytox, xtoy) +    return ret + + -    xs.add(x) -    q = [(x, None)] -    while len(q) > 0: -        (xx, yy) = q.pop(0) -        if xx is not None: -            for y in xtoy.pop(xx): -                if y not in ys: -                    ys.add(y) -                    q.append((None, y)) -        if yy is not None: -            for x in ytox.pop(yy): -                if x not in xs: -                    xs.add(x) -                    q.append((x, None)) -    ret += len(xs) * len(ys) -print((ret - n)) +    num = doit(x, xs, ys, xtoy, ytox) +    ret += len(xs) * len(ys) - num // 2 +print(ret)