#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define IINF 10e8
#define INF 1<<30
#define MOD 1000000007
#define mod 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
//using Int=int_fast64_t;
typedef long long int ll;
typedef long long int LL;
typedef pair<ll,ll>Pll;
typedef pair<int,int>Pin;
template<class T> using V=vector<T>;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
char dir[4]={'u','l','d','r'};
ll cmp(pair<ll,ll>a,pair<ll,ll> b){
        if(a.se!=b.se)
        return a.se<b.se;
        else
        return a.fi<b.fi;
}
//---------------------------------------------------------------------------
struct unionfind{
    vector<int> rank,parent,size;
    unionfind(int n){
        for(int i=0;i<n;i++){
            rank.push_back(1);//树的高度
            parent.push_back(i);//根
            size.push_back(1);//树大小
        }
    }
    int getroot(int x){
        if(parent[x]==x)
            return x;
        return parent[x]=getroot(parent[x]);//押平路径加速以后的查找
    }
    bool issame(int a,int b){
        return getroot(a)==getroot(b);
    }
    void unite(int a,int b){
        a=getroot(a);
        b=getroot(b);
        if(a==b)
            return;
        else if(rank[a]>rank[b]){ //合并到高的树下
            parent[b]=a;
            size[a]+=size[b];
        }
        else if(rank[a]==rank[b]){
            parent[b]=a;
            rank[a]++;
            size[a]+=size[b];
        }
        else{
            parent[a]=b;
            size[b]+=size[a];
        }
    }
};

class Edge{
public:
    int source,target,cost;
    Edge(int source=0,int target=0,int cost=0):
    source(source), target(target), cost(cost) {}
    bool operator < ( const Edge &e) const {
        return cost < e.cost;
    }
};

int kruskal(int n,vector<Edge> edges){
    int total=0;
    sort(all(edges));
    unionfind uf(n);
    for(int i=0;i<edges.size();i++){
        Edge e=edges[i];
        if(!uf.issame(e.source, e.target)){
            total+=e.cost;
            uf.unite(e.source,e.target);
        }
    }
    return total;
}

//---------------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    ll n;cin>>n;
    vector<Pll>vx,vy;
    for(int i=0;i<n;i++){
        ll x,y;cin>>x>>y;
        vx.pb({x,i});
        vy.pb({y,i});
    }
    sort(all(vx));sort(all(vy));
    vector<tuple<ll,ll,ll>>edges;
    for(int i=0;i<n-1;i++){
        edges.pb(make_tuple(vx[i+1].fi-vx[i].fi,vx[i].se,vx[i+1].se));
        edges.pb(make_tuple(vy[i+1].fi-vy[i].fi,vy[i].se,vy[i+1].se));
    }
    sort(all(edges));
    ll ans=0;
    unionfind uf(n);
    for(ll i=0;i<edges.size();i++){
        ll x,y,cost;
        cost=get<0>(edges[i]);
        x=get<1>(edges[i]);
        y=get<2>(edges[i]);
        if(!uf.issame(x,y)){
            uf.unite(x, y);
            ans+=cost;
        }
    }
    cout<<ans<<endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------
/*
int n;cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)
    cin>>a[i];
*/


Measured memory_bytes: 12896