// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define MAX 300000

#define int long long

struct Pair{

	int x,y, id;

};

int n;

Pair a[MAX+1];

typedef Pair PP;

typedef pair<int,int> pp;

vector<pair<int,int> > G[MAX+1];

bool cmp(PP a,PP b){return a.x<b.x;};

bool CMP(PP a,PP b){return a.y<b.y;};

bool visited[MAX+1];

int ans;

#define fi first

#define se second

void DJK(int x){

	ans = 0;

	priority_queue<pp,vector<pp> ,greater<pp> >pq;

	pq.push({0,x});

	while(!pq.empty()){

		int u = pq.top().se;

		int w = pq.top().fi;

		pq.pop();

		if(visited[u]) continue;

		ans+=w;

		visited[u] = 1;

		for(pp ii : G[u]){

			int v = ii.se;

			pq.push({ii.fi,v});

		}

	}

}

signed main(){

	cin >> n;

	for(int i=1;i<=n;i++){

		cin >> a[i].x >> a[i].y;

		a[i].id = i;

	}

	sort(a+1,a+n+1,cmp);

	for(int i=2;i<=n;i++){

		G[a[i].id].push_back({a[i].x-a[i-1].x,a[i-1].id});

		G[a[i-1].id].push_back({a[i].x-a[i-1].x,a[i].id});

	}

	sort(a+1,a+n+1,CMP);

	for(int i=2;i<=n;i++){

		G[a[i].id].push_back({a[i].y-a[i-1].y,a[i-1].id});

		G[a[i-1].id].push_back({a[i].y-a[i-1].y,a[i].id});

	}

	DJK(1);

	cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 9.291932072285265x):
// #define FNAME "template"



#define NDEBUG



#include <bits/stdc++.h>

using namespace std;



typedef long long inte;

const int maxn = 100005;



#ifndef NDEBUG

#    define dprint(x) (cerr << (#x) << " = "<< (x) << endl)

#else

#    define dprint(x) (NULL)

#endif



template<typename T>

inline void readFig(T& x)

{

#   ifdef FTREAD

        static int c = 0;

        int op = 1;

        x = 0;

        while (!isdigit(c)) {

            if (c == '-')

                op = -1;

            c = getchar();

        }

        while (isdigit(c)) {

            x *= 10;

            x += c - '0';

            c = getchar();

        }

        x *= op;

#   else

        cin >> x;

#   endif

}



int N, M;

struct Edge {

    int u, v, w;

} edges[maxn * 2];

inline bool operator < (const Edge &a, const Edge &b)

{

    return a.w < b.w;

}



int par[maxn];

int getPar(int x)

{

    static int st[maxn];

    int stop = 0;

    while (par[x] != x) {

        x = par[x];

        st[stop++] = x;

    }

    while (stop--)

        par[st[stop]] = x;

    return x;

}

inline inte genMST()

{

    for (int i = 1; i <= N; ++i)

        par[i] = i;

    int cnt = 0;

    inte ans = 0;

    for (int i = 1; i <= M; ++i) {

        int u = getPar(edges[i].u), v = getPar(edges[i].v);

        if (u != v) {

            par[u] = v;

            ++cnt;

            ans += edges[i].w;

            dprint(ans);

        }

    }

    //assert(cnt == N - 1);

    return ans;

}



struct City {

    int x, y, id;

} cities[maxn];



inline bool compx(const City &a, const City &b)

{

    return a.x < b.x;

}

inline bool compy(const City &a, const City &b)

{

    return a.y < b.y;

}

inline int getdis(const City &a, const City &b)

{

    return min(abs(a.x - b.x), abs(a.y - b.y));

}



inline void solve()

{

    readFig(N);

    for (int i = 1; i <= N; ++i) {

        readFig(cities[i].x); readFig(cities[i].y);

        cities[i].id = i;

    }

    sort(cities + 1, cities + N + 1, compx);

    for (int i = 1; i < N; ++i) {

        edges[++M].u = cities[i].id;

        edges[M].v = cities[i + 1].id;

        edges[M].w = getdis(cities[i], cities[i + 1]);

    }

    sort(cities + 1, cities + N + 1, compy);

    for (int i = 1; i < N; ++i) {

        edges[++M].u = cities[i].id;

        edges[M].v = cities[i + 1].id;

        edges[M].w = getdis(cities[i], cities[i + 1]);

    }

    sort(edges + 1, edges + M + 1);

    cout << genMST() << endl;

}



signed main()

{

#   ifdef FNAME

        freopen(FNAME".in", "r", stdin);

        freopen(FNAME".out", "w", stdout);

#   endif

#   ifndef FTREAD

    	ios::sync_with_stdio(false);

    	cin.tie(0);

#   endif



#   ifdef MULTEST

        int T;

        readFig(T);

        while (T--)

            solve();

#   else

        solve();

#   endif



#   ifdef FNAME

        fclose(stdin);

        fclose(stdout);

#   endif

    	return 0;

}