#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif

struct pack{
	int idx, x, y;
	bool operator<(const pack &a) const
	{
		return x < a.x;
	}
};
vector<vector<int> > vv;

bool comp(const pack &a, const pack &b)
{
	return a.y < b.y;
}


struct edge{
	int u, v, cost;
	bool operator<(const edge &a) const{
		return cost < a.cost;
	}
};
vector<edge> g;
class UnionFind{
    public:
        vector<ll> par; // 親
        vector<ll> rank; // 木の深さ
        ll numComponent; // 木の数
        void init(ll n){
            par.resize(n);
            rank.assign(n, 1LL);
            for(ll i=0; i<n; ++i) par[i] = i;
            numComponent = n;
        }

        ll findRoot(ll x){
            if(par[x] == x) return x;
            else return par[x] = findRoot(par[x]);
        }

        void unite(ll x, ll y){
            x = findRoot(x);
            y = findRoot(y);
            if(x == y) return;
            if(rank[x] < rank[y]) swap(x, y);
            rank[x] += rank[y];
            par[y] = x;
            numComponent--;
            return;
        }

        bool same(ll x, ll y){
            return findRoot(x) == findRoot(y);
        }

        ll size(ll x){
            return rank[findRoot(x)];
        }
};


int main(int argc, const char * argv[]) {
	std::cout << std::fixed << std::setprecision(15);

	int N; cin >> N;
	vector<pack> xy(N);
	rep(i, N){
		int x, y; cin >> x >> y;
		xy[i] = {i, x, y};
	}

	sort(ALL(xy));

	rep(i, N){
		// cout << xy[i].idx << " " << xy[i].x << " " << xy[i].y << endl;
		if(i!=N-1){
			g.push_back({xy[i].idx, xy[i+1].idx, abs(xy[i].x - xy[i+1].x)});
		}
	}

	sort(ALL(xy), comp);

	rep(i, N){
		// cout << xy[i].idx << " " << xy[i].x << " " << xy[i].y << endl;
		if(i!=N-1){
			g.push_back({xy[i].idx, xy[i+1].idx, abs(xy[i].y - xy[i+1].y)});
		}
	}
	// cout << endl;

	sort(ALL(g));
	UnionFind uf;
	uf.init(g.size());
	ll res = 0;
	for(int i=0; i<g.size(); i++){
		edge e = g[i];
		if(!uf.same(e.u, e.v)) {
			// cout << e.u <<" " <<e.v <<" " << e.cost << endl;
			uf.unite(e.u, e.v);
			res += e.cost;
		}
	}
	cout << res << endl;

	return 0;
}


Measured memory_bytes: 7536