//============================================================================
// Name        : hoba.cpp :)
// Author      : (O_O Diab) (Why_not) (el 3AQ)
// Version     :
// Copyright   : please, Don't touch me :(
// Description : problem (I) in C++
// MAIN IDEA   : max sum of S(X) + S(S(X)) => 100 so brute force
//============================================================================
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define rw freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
#define pb push_back
#define F first
#define S second
#define pf push_front
#define lp(i,a,b) for (int i=a;i<=(int)b;++i)
#define lp2(i,a,b) for (int i=a;i>=b;--i)
#define f(x) return cout<<x<<"\n",0
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define p(x) cout<<#x<<" = { "<<x<<" } \n"
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define EPS 1e-6
// to convert string to number use int a = stoi(string);
// to convert number to string use string s = to_string(int);
#define FAST ios::sync_with_stdio(0),ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define clr(c,x) memset(c,x,sizeof(c));
using namespace __gnu_pbds;
using namespace std;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
const int dy[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
const int N = 1e5 + 5, mod = (int) 1e9 + 7, INF = 0x3f3f3f3f;
set<pii>stx, sty;
vii v;
bool vis[N] = { };
int main() {
#ifdef ONLINE_JUDGE
    FAST
#endif
	int n;
	cin >> n;
	lp(i,0,n-1)
	{
		int x, y;
		cin >> x >> y;
		stx.insert( { x, i });
		stx.insert( { -x, i });
		sty.insert( { y, i });
		sty.insert( { -y, i });
		v.pb( { x, y });
	}
	ll sum = 0;
	lp(i,0,n-1)
	{
		if (vis[i])
			continue;
		stx.erase( { v[i].F, i });
		stx.erase( { -v[i].F, i });
		sty.erase( { v[i].S, i });
		sty.erase( { -v[i].S, i });
		int fx = INT_MAX, sx = INT_MAX, fy = INT_MAX, sy = INT_MAX;
		int idx_fx, idx_sx, idx_fy, idx_sy;
		auto it = stx.lower_bound( { v[i].F, 0 });
		pii po;
		// positive x ->fx
		if (it != stx.end()) {
			po = *it;
			if (po.F < 0)
				po.F *= -1;
			fx = abs(po.F - v[i].F);
			idx_fx = po.S;
		}
		// negative x ->sx
		it = stx.lower_bound( { -v[i].F, 0 });
		if (it != stx.end()) {
			po = *it;
			if (po.F < 0)
				po.F *= -1;
			sx = abs(po.F - v[i].F);
			idx_sx = po.S;
		}
		// positive y ->fy
		it = sty.lower_bound( { v[i].S, 0 });
		if (it != sty.end()) {
			po = *it;
			if (po.F < 0)
				po.F *= -1;
			fy = abs(po.F - v[i].S);
			idx_fy = po.S;
		}
		// negative y ->sy
		it = sty.lower_bound( { -v[i].S, 0 });
		if (it != sty.end()) {
			po = *it;
			if (po.F < 0)
				po.F *= -1;
			sy = abs(po.F - v[i].S);
			idx_sy = po.S;
		}
		int mn = min(min(fx, sx), min(fy, sy));
		if (mn == fx) {
			vis[idx_fx] = true;
			sum += fx;
			p(idx_fx);
		} else if (mn == sx) {
			vis[idx_sx] = true;
			sum += sx;
			p(idx_sx);
		} else if (mn == fy) {
			vis[idx_fy] = true;
			sum += fy;
			p(idx_fy);
		} else if (mn == sy) {
			vis[idx_sy] = true;
			sum += sy;
			p(idx_sy);
		}
		stx.insert( { v[i].F, i });
		stx.insert( { -v[i].F, i });
		sty.insert( { v[i].S, i });
		sty.insert( { -v[i].S, i });
//		p(i);
		//p(fx);
	//	p(sx);
		//p(fy);
		//p(sy);
		//cout << "\n=========\n";
	}
	f(sum);
	return 0;
}
