#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
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; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT { INIT(){
  cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;

class UnionFind {
  public:
    vector <int> par;
    vector <int> siz;

    UnionFind(int sz_): par(sz_), siz(sz_, 1LL) {
      for (int i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(int sz_) {
      par.resize(sz_);
      siz.assign(sz_, 1LL);
      for (int i = 0; i < sz_; ++i) par[i] = i;
    }
    int root(int x) {
      while (par[x] != x) {
        x = par[x] = par[par[x]];
      }
      return x;
    }
    bool unite(int x, int y) {
      x = root(x);
      y = root(y);
      if (x == y) return false;
      if (siz[x] < siz[y]) swap(x, y);
      siz[x] += siz[y];
      par[y] = x;
      return true;
    }
    bool same(int x, int y) {
      return root(x) == root(y);
    }
    int size(int x) {
      return siz[root(x)];
    }
};


template<typename T>
T kruskal(vector<vector<pair<int, int>>> &graph, int N){
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> q;
  // to from cost
  REP(i, N) REP(j, graph[i].size()){
    q.push(make_pair(graph[i][j].second, make_pair(i, graph[i][j].first)));
  }
  UnionFind uf(N);
  T res = 0;
  while(!q.empty()){
    auto p = q.top(); q.pop();
    if(uf.unite(p.second.first, p.second.second)){
      res += p.first;
    }
  }
  return res;
}

struct P {
  int x, y, n;
};

signed main() {
  int N; 
  cin >> N;

  vector<P> A(N);
  REP(i, N){
    cin >> A[i].x >> A[i].y;
    A[i].n = i;
  }

  vector<vector<pair<int, int>>> graph(N);

  sort(ALL(A), [](P const & l, P const & r){
      return l.x < r.x;
      });

  REP(i, N-1){
    graph[A[i].n].emplace_back(A[i+1].n, abs(A[i].x-A[i+1].x));
  }

  sort(ALL(A), [](P const & l, P const & r){
      return l.y < r.y;
      });

  REP(i, N-1){
    graph[A[i].n].emplace_back(A[i+1].n, abs(A[i].y-A[i+1].y));
  }

  cout << kruskal<int>(graph, N) << "\n";

  return 0;
}

Measured memory_bytes: 18800