#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define mt make_tuple
#define foreach(it, a) for(__typeof(a.begin()) it=a.begin(); it!=a.end(); it++)
#define forn(i, a) for(int i=0; i<a; i++)
#define INF 1e9
#define all(x) x.begin(), x.end()
#define MOD 1e9+7 

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<pi,int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef unsigned int uint;

int N, par[100005], ranks[100005], a, b, cnt=0;
pi town[100005];
ll res=0;

struct Node
{
	ll cost;
	int x;
	int y;
	Node(ll a, int b, int c)
	{
		cost=a;
		x=b;
		y=c;
	}
	Node(){}
}fin[90000000];

inline bool cmp(const Node&a, const Node&b)
{
	return a.cost<b.cost;
}

void init()
{
	for(int i=1; i<=N; i++)
		par[i]=i, ranks[i]=0;
}

inline int find(int x)
{
	return par[x]==x?x:par[x]=find(par[x]);
}

inline bool unite(int x, int y)
{
	x=find(x), y=find(y);
	if(x==y)
		return  false;
	if(ranks[x]<ranks[y])
		par[x]=y;
	else
	{
		par[y]=x;
		if(ranks[x]==ranks[y])
			ranks[x]++;
	}
	return true;
}

int main() 
{
	scanf("%d", &N);
	init();
	for(int i=1; i<=N; i++)
		scanf("%d %d", &town[i].f, &town[i].s);
	
	for(int i=1; i<N; i++)
		for(int j=i+1; j<=N; j++)
			fin[cnt++]=Node((ll)min(abs(town[i].f-town[j].f), abs(town[i].s-town[j].s)), i, j);
		
	sort(fin, fin+cnt, cmp);
	for(int i=0; i<cnt; i++)
		res+=fin[i].cost*(unite(fin[i].x, fin[i].y));
	cout<<res<<endl;
	return 0;
}