//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;

    set<int> st[100001];
    set<int> st2[100001];

    // Disjoint Set DataStructure
    int parent[100001];         // reserve space for map if n > 10000
    int urank[100001];

    void create(ll x)
    {
        parent[x] = x ;
        urank[x] = 1 ;         // rank = no. of nodes in its subtree
    }

    ll find(ll x)
    {
        if( parent[x] != x )    //path compression
        {
            parent[x] = find(parent[x]) ;
        }
        return parent[x];
    }

    void merge(ll x, ll y)
    {
        ll xroot = find(x);
        ll yroot = find(y);
        if(xroot==yroot)
            return;
        if( urank[xroot] <= urank[yroot] )    // Union by rank
        {
            parent[xroot] = yroot ;
            urank[yroot] = urank[yroot] + urank[xroot] ;
        }
        else
        {
            parent[yroot] = xroot;
            urank[xroot] = urank[xroot] + urank[yroot] ;
        }
    }

    vector<int> v[200001];

    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            int n,i,j,a,b,x,y;
            cin>>n;
            for(i=1;i<=100000;++i)
                create(i);
            for(i=1;i<=n;++i)
            {
                cin>>a>>b;
                st[b].insert(a);
                st2[a].insert(b);
            }
            ll ans=0;
            for(i=1;i<=100000;++i)
            {
                if(st[i].size()<=1)
                    continue;
                auto it=st[i].begin();
                auto it2=st[i].begin();
                ++it2;
                while(it2!=st[i].end())
                {
                    merge(*it2,*it);
                    ++it2;
                    ++it;
                }
            }
            for(i=1;i<=100000;++i)
            {
                //cout<<i<<" "<<find(i)<<endl;
                v[find(i)].pb(i);
            }
            set<int> st3;
            for(i=1;i<=100000;++i)
            {
                if(v[i].size()<=1)
                    continue;
                //cout<<i<<" "<<v[i].size()<<endl;
                st3.clear();
                for(j=0;j<v[i].size();++j)
                {
                    for(auto it:st2[v[i][j]])
                        st3.insert(it);
                }
                for(j=0;j<v[i].size();++j)
                {
                    ans=ans+((int)st3.size()-(int)st2[v[i][j]].size());
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }


Measured memory_bytes: 30392