#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <math.h>
 
using namespace std;
 
using LL = long long;
using VLL = std::vector<LL>;
using VVLL = std::vector<VLL>;
using VVVLL = std::vector<VVLL>;
using LD = long double;
using VLD = std::vector<LD>;
using VVLD = std::vector<VLD>;
using VVVLD = std::vector<VVLD>;
using BL = bool;
using VBL = std::vector<BL>;
using VVBL = std::vector<VBL>;
using VVVBL = std::vector<VVBL>;


template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; }

//template<class T=LL> inline vector<T> cinv(LL N){ vector<T> v(N); REP(i, N)cin>>v[i]; return move(v);}
//template<class T=LL> inline vector<T> cinv(LL N){ vector<T> v(N); REP(i, N)cin>>v[i]; return move(v);}
#define REP(i,n) for(LL (i)=0;(i)<(n);(i)++)
#define REPM(i,n,m) for(LL (i)=m;(i)<(n);(i)++)
#define ALL(v) v.begin(), v.end()
#define PRINTLN(v) {LL i=0; for(auto (a): v ){   cerr <<i << " : "<<  a << endl; i++;}}
//#define PRINTSP(v) for(auto (a): v ) {cerr << a << " ";} cerr << endl

template<class T> 
void PRINTSP(vector<T> v, size_t w=3){
  for(auto (a): v ) {cerr <<setw(w)<< a << " ";} cerr << endl;
} 

template<typename TTT>
vector<LL> arg_sort(vector<TTT> A, bool ascend = true) {
  vector<LL> index(A.size());
  iota(index.begin(), index.end(), 0);
  if (ascend) {
    std::sort(index.begin(), index.end(), [&A](TTT i1, TTT i2) {
      return A[i1] < A[i2];
      });
  }
  else {
    std::sort(index.begin(), index.end(), [&A](TTT i1, TTT i2) {
      return A[i1] > A[i2];
      });
  }

  return index;
}

struct UinonFindTree {
private:
  vector<LL> UFT;
public:
  UinonFindTree(LL N) {
    UFT.resize(N);
    for (LL i = 0; i < N; i++) {
      UFT[i] = -1; 
    }
  }

  LL root(LL i) { 
    if (UFT[i] < 0) {
      return i;
    }
    else {
      LL j = root(UFT[i]);
      UFT[i] = j;
      return j;
    }
  }

  bool same(LL i, LL j) {
    return (root(i) == root(j));
  }

  void unite(LL i, LL j)
  {
    LL root_i = root(i);
    LL root_j = root(j);
    if (root_i != root_j) {
      if (size(root_i) < size(root_j)) {
        swap(root_i, root_j);
      }
      UFT[root_i] += UFT[root_j];
      UFT[root_j] = root_i;
    }
  }

  LL size(LL i) {
    return -UFT[root(i)];
  }

  LL get_root_num() {
    set<LL> roots;
    for (LL i = 0; i < UFT.size(); ++i) {
      roots.insert(root(i));
    }
    return (LL)roots.size();
  }

  map<LL, vector<LL>> get_root_child() {
    map<LL, vector<LL>> a;
    for (LL i = 0; i < UFT.size(); ++i) {
      LL j = root(i);
      a[j].push_back(i);
    }
    return a;
  }

  void print() { 
    for (LL i = 0; i < UFT.size(); ++i) {
      cerr << root(i) << " ";
    }
    cerr << endl;
  }
};



#define cmax(a,b) {if(a<b){a=b;}}
#define cmin(a,b) {if(a>b){a=b;}}
#define EACH(a,A) for(auto a: A)
//#define cmax(a,b) a = (a>b ? a:b)
//#define cmin(a,b) a = (a<b ? a:b) 
//cin,cout高速化のおまじない＋桁数指定
inline VLL cinvll(LL N, LL minus = 0){
  VLL A(N);
  REP(i,N){
    cin >> A[i];
    A[i] -= minus;
  }
  return move(A);
}
inline VVLL zerosll(LL H, LL W, LL val=0){
  VVLL A(H,VLL(W,val));
  return move(A);
}
struct Fast{
	Fast(){
		cin.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
	}
} fast;


void func(){
  LL N;
  cin >> N;
  VLL X(N);
  VLL Y(N);
  
  REP(i,N){
    LL x,y; 
    cin >> x;
    cin >> y;
    X[i] = x;
    Y[i] = y;
  }

  //PRINTSP(X);
  //PRINTSP(Y);

  auto X_index = arg_sort(X);
  auto Y_index = arg_sort(Y);
  //PRINTSP(X_index);
  //PRINTSP(Y_index);

  VVVLL A(N);
  vector<set<vector<LL>>> S(N);
  REP(i,N-1){
    LL i0 = X_index[i];
    LL i1 = X_index[i+1];
    auto distx = abs(X[i0]-X[i1]);
    auto disty = abs(Y[i0]-Y[i1]);
    auto dist = min(distx,disty);
    S[i0].insert({i1,dist});
    //S[i1].insert({i0,dist});

  }
  REP(i,N-1){
    LL i0 = Y_index[i];
    LL i1 = Y_index[i+1];
    auto distx = abs(X[i0]-X[i1]);
    auto disty = abs(Y[i0]-Y[i1]);
    auto dist = min(distx,disty);
    S[i0].insert({i1,dist});
    //S[i1].insert({i0,dist});
  }

  /*
  REP(i,N){
    auto a = S[i];
    cerr << i << " : ";
    for(auto b : a){
      cerr << "{" << b[0] << "," << b[1] <<"}" ;
    }
    cerr << endl;
  }
*/
  priority_queue<vector<LL>,VVLL ,std::greater<VLL>> Q;
  REP(i,N){
    auto a = S[i];
    for(auto b : a){
      Q.push({b[1],i,b[0]});     
    }
  }

  UinonFindTree UTF(N);
  LL total_cost = 0;
  while(not Q.empty()){
    auto q = Q.top();
    Q.pop();
    //PRINTSP(q);
    LL cost = q[0];
    LL a = q[1];
    LL b = q[2];
    if(not UTF.same(a,b)){
      UTF.unite(a,b);
      total_cost +=cost;
    }
  }

  cout << total_cost;

}
int main()
{
  func();
  return 0;
}
 

Measured memory_bytes: 42344