#include <stdlib.h>
#include <stdio.h>

int main ()
{

    long N;
    scanf ("%ld", &N);

    long ri, ci;
    int* R = (int*)calloc (N, sizeof(int));
    int* C = (int*)calloc (N, sizeof(int));

    for (long i=0; i<N; i++)
    {
        scanf ("%ld%ld", &ri, &ci);
        R[ri-1]++;
        C[ci-1]++;
    }

    long t = 0; long long s = 0;

    for (int i=0; i<N; i++)
    {
        t++;
        if (C[i])
        {
            if (C[i]>=t)
            {
                s += ((long long)t)*((long long)(t-1))/2;
                C[i] -= t;
                t = 0;
                s += (long long)C[i];
                if (i+1<N)
                    C[i+1] += C[i];
            }
            else
            {
                s += ((long long)(C[i]))*((long long)(C[i]+1))/2 + ((long long)C[i])*((long long)(t-C[i]));
                t -= C[i] - 1;
            }
        }
    }

    t = 0;
    for (int i=0; i<N; i++)
    {
        t++;
        if (R[i])
        {
            if (R[i]>=t)
            {
                s += ((long long)t)*((long long)(t-1))/2;
                R[i] -= t;
                t = 0;
                s += (long long)R[i];
                if (i+1<N)
                    R[i+1] += R[i];
            }
            else
            {
                s += ((long long)(R[i]))*((long long)(R[i]+1))/2 + ((long long)R[i])*((long long)(t-R[i]));
                t -= R[i] - 1;
            }
        }
    }

    printf ("%lld", s);

    return 0;
}