#include <iostream>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;
using ll = long long;

const int MAX = 1e5+1;
ll G[MAX][MAX];
int N;

ll prim(){
    ll ans = 0ll;
    vector<bool> used(N,false);
    priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > Q;

    Q.push(make_pair(0,0));
    while(!Q.empty()){
        int u;
        ll cost_u;
        tie(cost_u, u) = Q.top(); Q.pop();

        if(used[u]) continue;
        used[u] = true;
        ans += cost_u;
        for(int j=0;j<N;j++){
            Q.push(make_pair(G[u][j], j));
        }
    }
    return ans;
}

int main(){
    cin >> N;
    vector<pair<int,int> > towns(N);
    int x,y;
    for(int i=0;i<N;i++){
        scanf("%d%d", &x,&y);
        towns[i] = make_pair(x,y);
    }
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            ll cost = min(abs(towns[i].first - towns[j].first), abs(towns[i].second-towns[j].second));
            G[i][j] = cost;
        }
    }
    cout << prim() << endl;
    return 0;
}