#pragma GCC optimize("Ofast,unroll-loops") #include using namespace std; map>m; int dist(int x, int y, int u, int v){ return abs(x-u)+abs(y-v); } int main(){ long long sum=0; int cnt, x,y; cin >> cnt; while(cnt--){ cin >> x >> y; if(m.find(x)!=m.end()) m[x] = {min(m[x].first, y), max(m[x].second, y)}; else m[x] = {y,y}; } for(auto&x:m){ sum+=abs(x.second.first-x.second.second); } for(auto it1=m.begin(), it2=next(m.begin());it2!=m.end();++it1,++it2){ sum+=min(min(min(dist(it1->first,it1->second.first, it2->first,it2->second.first), dist(it1->first,it1->second.second, it2->first,it2->second.first)), dist(it1->first,it1->second.first, it2->first,it2->second.second)), dist(it1->first,it1->second.second, it2->first,it2->second.second)); } cout << sum << "\n"; }