#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool func(const pair<int,int> &a,const pair<int,int> &b)
{
if(a.first<=b.first)return true;
return false;
}
int main()
{
int n;
cin>>n;
vector<pair<int,int> >x(n);
vector<pair<int,int> >y(n);
///v.first ->x , v.second ->y
for(int i=0;i<n;i++){
cin>>x[i].first;
cin>>y[i].first;
x[i].second=i;
y[i].second=i;
}
sort(x.begin(),x.end(),func);
int d[n];
d[x[0].second]=0;
for(int i=1;i<n;i++)
{
if(x[i].first>x[i-1].first)
d[x[i].second]=d[x[i-1].second]+1;
else ///x[i].first==x[i-1].first
d[x[i].second]=d[x[i-1].second];
}
sort(y.begin(),y.end(),func);
int d2[n];
d2[y[0].second]=0;
for(int i=1;i<n;i++)
{
if(y[i].first>y[i-1].first)
d2[y[i].second]=d2[y[i-1].second]+1;
else ///y[i].first==y[i-1].first
d2[y[i].second]=d2[y[i-1].second];
}
int s=0;
for(int i=0;i<n;i++)
s+=min(d[i],d2[i]);
cout<<s;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKYm9vbCBmdW5jKGNvbnN0IHBhaXI8aW50LGludD4gJmEsY29uc3QgcGFpcjxpbnQsaW50PiAmYikKewogICAgaWYoYS5maXJzdDw9Yi5maXJzdClyZXR1cm4gdHJ1ZTsKICAgIHJldHVybiBmYWxzZTsKCn0KaW50IG1haW4oKQp7CiAgICBpbnQgbjsKICAgIGNpbj4+bjsKICAgIHZlY3RvcjxwYWlyPGludCxpbnQ+ID54KG4pOwogICAgdmVjdG9yPHBhaXI8aW50LGludD4gPnkobik7CiAgICAvLy92LmZpcnN0IC0+eCAsIHYuc2Vjb25kIC0+eQogICAgZm9yKGludCBpPTA7aTxuO2krKyl7CiAgICAgICAgY2luPj54W2ldLmZpcnN0OwogICAgICAgIGNpbj4+eVtpXS5maXJzdDsKICAgICAgICB4W2ldLnNlY29uZD1pOwogICAgICAgIHlbaV0uc2Vjb25kPWk7CiAgICB9CiAgICBzb3J0KHguYmVnaW4oKSx4LmVuZCgpLGZ1bmMpOwogICAgaW50IGRbbl07CiAgICBkW3hbMF0uc2Vjb25kXT0wOwogICAgZm9yKGludCBpPTE7aTxuO2krKykKICAgIHsKICAgICAgICBpZih4W2ldLmZpcnN0PnhbaS0xXS5maXJzdCkKICAgICAgICBkW3hbaV0uc2Vjb25kXT1kW3hbaS0xXS5zZWNvbmRdKzE7CiAgICAgICAgZWxzZSAgICAvLy94W2ldLmZpcnN0PT14W2ktMV0uZmlyc3QKICAgICAgICBkW3hbaV0uc2Vjb25kXT1kW3hbaS0xXS5zZWNvbmRdOwogICAgfQogICAgc29ydCh5LmJlZ2luKCkseS5lbmQoKSxmdW5jKTsKICAgIGludCBkMltuXTsKICAgIGQyW3lbMF0uc2Vjb25kXT0wOwogICAgZm9yKGludCBpPTE7aTxuO2krKykKICAgIHsKICAgICAgICBpZih5W2ldLmZpcnN0PnlbaS0xXS5maXJzdCkKICAgICAgICBkMlt5W2ldLnNlY29uZF09ZDJbeVtpLTFdLnNlY29uZF0rMTsKICAgICAgICBlbHNlICAvLy95W2ldLmZpcnN0PT15W2ktMV0uZmlyc3QKICAgICAgICBkMlt5W2ldLnNlY29uZF09ZDJbeVtpLTFdLnNlY29uZF07CiAgICB9CiAgICBpbnQgcz0wOwogICAgZm9yKGludCBpPTA7aTxuO2krKykKICAgICAgICBzKz1taW4oZFtpXSxkMltpXSk7CiAgICBjb3V0PDxzOwogICAgcmV0dXJuIDA7Cn0K