#include<bits/stdc++.h>
using namespace std;

#define int long long

typedef  pair<int, int> iPair;
struct Graph
{
    int V, E;
    vector< pair<int, iPair> > edges;
    
    Graph(int V, int E)
    {
        this->V = V;
        this->E = E;
    }
    void addEdge(int u, int v, int w)
    {
        edges.push_back({w, {u, v}});
    }
    int kruskalMST();
};

vector< iPair > edges2;
bool cmp1(int a, int b){
	if(edges2[a].first > edges2[b].first || (edges2[a].second > edges2[b].second && edges2[a].first == edges2[b].first)) return true;
	else return false;
}

bool cmp2(int a, int b){
	if(edges2[a].second > edges2[b].second || (edges2[a].first > edges2[b].first && edges2[a].second == edges2[b].second)) return true;
	else return false;
}

struct DisjointSets
{
    int *parent, *rnk;
    int n;
    DisjointSets(int n)
    {
        this->n = n;
        parent = new int[n+1];
        rnk = new int[n+1];
        for (int i = 0; i <= n; i++)
        {
            rnk[i] = 0;
            parent[i] = i;
        }
    }
    int find(int u)
    {
        if (u != parent[u])
            parent[u] = find(parent[u]);
        return parent[u];
    }

    void merge(int x, int y)
    {
        x = find(x), y = find(y);
        if (rnk[x] > rnk[y])
            parent[y] = x;
        else
            parent[x] = y;

        if (rnk[x] == rnk[y])
            rnk[y]++;
    }
};

int Graph::kruskalMST()
{
    int mst_wt = 0;
    sort(edges.begin(), edges.end());
    DisjointSets ds(V);
    vector< pair<int, iPair> >::iterator it;
    for (it=edges.begin(); it!=edges.end(); it++)
    {
        int u = it->second.first;
        int v = it->second.second;

        int set_u = ds.find(u);
        int set_v = ds.find(v);
        if (set_u != set_v)
        {
            cout << u << " - " << v << endl;
            mst_wt += it->first;
            ds.merge(set_u, set_v);
        }
    }

    return mst_wt;
}

int main()
{
	int n, x, y;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> x >> y;
		edges2.push_back(make_pair(x, y));
	} 
    Graph(n, 2*(n-1));
    sort(edges2, edges2 + n, cmp1);
    for(int i = 0; i < n-1; i++) Graph.push_back(i, i + 1, min(abs(edges2[i].first-edges2[i+1].first), abs(edges2[i].second-edges2[i+1].second));
    sort(edges2, edges2 + n, cmp2);
    for(int i = 0; i < n-1; i++) Graph.push_back(i, i + 1, min(abs(edges2[i].first-edges2[i+1].first), abs(edges2[i].second-edges2[i+1].second));
    int mst_wt = g.kruskalMST();
    cout << mst_wt;
    return 0;
}