#include <iostream>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

const int MAX = 100000;
const int WHITE = 0;
const int BLACK = 1;
const int GRAY = 2;
const int INFTY = (int) 1 << 21;

typedef pair<int,int> t_pi;

int n;
int list[MAX][MAX];
int d[MAX], color[MAX], p[MAX];
t_pi pos[MAX];

int cost_func(t_pi a, t_pi b){
    int A = abs(a.first-b.first);
    int B = abs(a.second-b.second);
    return min(A,B);
}

int prim(){
    for(int i=0;i<n;i++){
        color[i] = WHITE;
        p[i] = -1;
        d[i] = INFTY;
    }

    d[0] = 0;

    while(true){
        int mincost = INFTY;
        int u = -1;
        for(int i=0;i<n;i++){
            if(color[i] != BLACK && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }

        if(u == -1) break;
        color[u] = BLACK;
        for(int v=0;v<n;v++){
            if(color[v] != BLACK ){
                if(d[v] > list[u][v]){
                    d[v] = list[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }

    int total = 0;
    for(int i=0;i<n;i++){
        if(p[i] != -1){
            total += list[i][p[i]];
        }
    }

    return total;
}

int main(void){
    cin >> n;

    int x, y;
    for(int i=0;i<n;i++){
        cin >> x >> y;
        pos[i] = make_pair(x,y);
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int tmp = cost_func(pos[i],pos[j]);
            list[i][j] = tmp;
        }
    }

    cout << prim() << endl;

    return 0;
}

