fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val p = (1..n).map { val a = (readLine()!!.split(" ").map { it.toInt() }); Point(it-1, a[0], a[1]) }

    val xy = Array(100001) { mutableListOf<Int>() }
    val yx = Array(100001) { mutableListOf<Int>() }
    val uf = UnionFind(n)
    p.forEach {
        xy[it.x].add(it.id)
        yx[it.y].add(it.id)
    }

    xy.filter { it.size>=2 }.forEach { it.reduce{ a,b -> uf.unite(a,b); b } }
    yx.filter { it.size>=2 }.forEach { it.reduce{ a,b -> uf.unite(a,b); b } }

    val sum = p.groupBy { uf.find(it.id) }.map { it.value }.map {
        val x = it.distinctBy { it.x }.size
        val y = it.distinctBy { it.y }.size
        1L *x * y
    }.sum()

    println(sum-n)

}

data class Point(val id: Int, val x: Int, val y: Int) 

class UnionFind(N: Int) {
    private val parent = IntArray(N) { it }
    val roots = (0..N-1).toList().toMutableSet()
    private val rank = IntArray(N) { 0 }
    private val size = IntArray(N) { 1 }
    private var numTree = N

    fun size() = numTree
    fun find(x: Int): Int = if (parent[x] == x) x else { parent[x] = find(parent[x]); parent[x] }

    fun same(x: Int, y: Int) = find(x) == find(y)
    fun sizeOf(x: Int) = size[find(x)]
    fun unite(x: Int, y: Int) {
        val rx = find(x)
        val ry = find(y)
        if (rx == ry) return else numTree--

        if (rank[rx] < rank[ry]) {
            parent[rx] = ry
            roots.remove(rx)
            size[ry] += size[rx]
        } else {
            parent[ry] = rx
            roots.remove(ry)
            size[rx] += size[ry]
            if (rank[rx] == rank[ry]) rank[rx]++
        }
    }
}