/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

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

typedef long long ll;
#define tr(container, it) \
        for (auto it = container.begin(); it != container.end(); it++)
#define scontains(c, x) ((c).find(x) != (c).end())   //O(log n)
#define contains(c, x) (find((c).begin(),(c).end(),x) != (c).end()) //O(n)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define mll map<ll,ll>
#define intv(x, a, b)((x)>=a && (x)<=b)
#define  rep(i, _begin, _end) for (__typeof(_end) end = _end, begin = _begin,  i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define  _for(i, end) for (__typeof(end) i = 0, _len = (end); i < (_len); i += 1)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define F first
#define S second
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev
#define what_is(x) cerr << #x << " is " << x << endl;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define readint ({int t;cin >> t;t;})
const double PI = 2 * acos(.0);
const int INF = 0x3f3f3f3f;
const ll LLINF = 1000000000000000005LL;;
const ll MOD = (ll) (1e9) + 7;
void _mod(ll &x){x = mod(x, MOD);}
//const ll MOD = (ll) 998244353 ;
const double EPS = 1e-10;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *) array, (T *) (array + N), val);
}

struct edge {
    int src, dst;
    int weight;
};
struct graph {
    int n;
    vector<edge> edges;
    graph(int n = 0) : n(n) { }
    void add_edge(int src, int dst, int weight) {
        n = max(n, max(src, dst)+1);
        edges.push_back({src, dst, weight});
    }
    vector<int> p;
    int root(int i) {
        return p[i] < 0 ? i : p[i] = root(p[i]);
    }
    bool unite(int i, int j) {
        if ((i = root(i)) == (j = root(j))) return false;
        if (p[i] > p[j]) swap(i, j);
        p[i] += p[j]; p[j] = i;
        return true;
    }
    int kruskal() {
        p.assign(n, -1);
        sort(all(edges), [](edge x, edge y) {
            return x.weight < y.weight;
        });
        int result = 0;
        for (auto e: edges)
            if (unite(e.src, e.dst))
                result += e.weight;
        return result;
    }
};


//#define int ll


class DBuilt {
public:

void solve(std::istream& cin, std::ostream& cout) {
    int N;
    cin >> N;
    struct Point{
        int x,y,id;
    }Points[1101010];
    graph G(404040);
    _for(i,N){
        cin >> Points[i].x >> Points[i].y;
        Points[i].id = i;
    }
    sort(Points, Points+N,[](Point p1, Point p2){return p1.x < p2.x;});
    G.add_edge(Points[0].id, Points[1].id,abs(Points[0].x - Points[1].x));
    G.add_edge(Points[1].id, Points[0].id,abs(Points[1].x - Points[0].x));
    rep(i,1,N-1)G.add_edge(Points[i].id, Points[i+1].id,abs(Points[i].x - Points[i+1].x)),
                G.add_edge(Points[1+i].id, Points[i].id,abs(Points[i].x - Points[i+1].x));
    G.add_edge(Points[N-1].id, Points[N].id, abs(Points[N-1].x - Points[N].x)),
            G.add_edge(Points[N].id, Points[N-1].id, abs(Points[N-1].x - Points[N].x));

    sort(Points, Points+N,[](Point p1, Point p2){return p1.y < p2.y;});
    G.add_edge(Points[0].id, Points[1].id,abs(Points[0].y - Points[1].y));
    G.add_edge(Points[1].id, Points[0].id,abs(Points[1].y - Points[0].y));
    rep(i,1,N-1)G.add_edge(Points[i].id, Points[i+1].id,abs(Points[i].y - Points[i+1].y)),
                G.add_edge(Points[1+i].id, Points[i].id,abs(Points[i].y - Points[i+1].y));
    G.add_edge(Points[N-1].id, Points[N].id, abs(Points[N-1].y - Points[N].y)),
            G.add_edge(Points[N].id, Points[N-1].id, abs(Points[N-1].y - Points[N].y));
    cout << G.kruskal() << endl;





}


};


#undef int
int main() {
	DBuilt solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	in.tie(0); ios::sync_with_stdio(0); out<<fixed<<setprecision(20);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 11248