#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<algorithm>

#define rep(n) for(int i=0;i<n;i++)
#define repp(j, n) for(int j=0;j<n;j++)
#define reppp(i, m, n) for(int i=m;i<=n;i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define MOD 1000000007
#define MAX 100005
#define INF 1410065408
#define EPS 1e-9
 
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge{int from, to; ll cost;};

ll cost[MAX][MAX];
ll mincost[MAX];
priority_queue<Pll, vector<Pll>, greater<Pll> > que;
bool used[MAX];
int V;

ll prim(){
  fill(mincost, mincost+V, INF);
  fill(used, used+V, false);
  que.push(make_pair(0, 0));
  int res = 0;

  while(true){
	Pll v;
	while(!que.empty()){
	  if(!used[que.top().second]){
		v = que.top();
		break;
	  }
	  que.pop();
	}

	if(que.empty()) break;
	used[v.second] = true;
	res += v.first;

	rep(V){
	  mincost[i] = min(mincost[i], cost[v.second][i]);
	  que.push(make_pair(mincost[i], i));
	}
  }
  
  return res;
}

signed main(){
    scanf("%d", &V);
    vector<ll> x(V), y(V);
    rep(V){
        scanf("%lld %lld", &x[i], &y[i]);
        repp(j, i){
            cost[i][j] = cost[j][i] = min(abs(x[i] - x[j]), abs(y[i] - y[j]));
        }
    }
    printf("%lld\n", prim());
}