#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>


using namespace std;

struct edge
{
	int u,v,cost;
};

int n;
int x[10010],y[10010];
static int par[11010],rank[10010];
vector<edge> ki;
long long ans;

void init_union_find(int n)
{
	for(int i = 0;i < n;i++)
	{
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x)
{
	if(par[x] == x)
	{
		return x;
	}
	else
	{
		return par[x] = find(par[x]);
	}
}

void unite(int x,int y)
{
	x = find(x);
	y = find(y);
	if(x == y) return;
	
	if(rank[x] < rank[y]){
		par[x] = y;
	} else{
		par[y] = x;
		if(rank[x] == rank[y])rank[y]++;
	}
}

bool same(int x,int y)
{
	return find(x) == find(y);
}

bool comp(const edge& e1,const edge& e2)
{
	return e1.cost < e2.cost;
}

int kruskal()
{
	sort(ki.begin(),ki.end(),comp);
	init_union_find(n);
	int res = 0;
	for(int i = 0;i < ki.size();i++)
	{
		edge e = ki[i];
		if(!same(e.u,e.v))
		{
			unite(e.u,e.v);
			res += e.cost;
		}
	}
	return res;
}




int main()
{
	cin >> n;
	for(int i = 0;i < n;i++)
	{
		cin >> x[i] >> y[i];
	}
	for(int i = 0;i < n;i++)
	{
		for(int j = 0;j < n;j++)
		{
			edge k;
			k.u = i;k.v = j;
			k.cost = min(abs(x[i]-x[j]),abs(y[i]-y[j]));
//			cout << k.cost << endl;
			ki.push_back(k);
		}
	}
	
	ans = kruskal();
	
	cout << ans << endl;
	
	return 0;
}