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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
  const int MAX_V = 1000000;
  struct Edge{
    int to;
    long long cost;
  };
  struct NODE{
    int pos;
    long long cost;
  };
  bool operator < (const NODE &a,const NODE &b){
    return a.cost > b.cost;
  }
  //infで初期化
  vector<vector<ll>> cost;
  vector<Edge> g[100000],rg[100000];
  ll mincost[MAX_V];
  bool used[MAX_V];
  ll V;

  ll Prim(){
    ll res = 0;
    for(ll i = 0; i < V; ++i){
      mincost[i] = inf;
      used[i] = false;
    }
    priority_queue<NODE> q;
    q.push({0,0});
    while(q.size()){
      auto v = q.top();q.pop();
      if(mincost[v.pos] == inf){
        mincost[v.pos] = v.cost;
        res += v.cost;
        for(auto n : g[v.pos]){
          q.push({n.to,n.cost});
        }
      }
    }
    return res;
  }
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,x,y;
  cin >> n;
  vector<pll> v;
  set<pll> s;
  rep(i,n){
    cin >> x >> y;
    s.insert(mp(x,y));
  }
  for(auto u : s){
    v.emplace_back(u);
  }
  n = v.size();
  V = n;
  rep(k,n){
    x = v[k].fi,y = v[k].se;
    ll dis = inf,tmp;
    ll cnt = 0;
    rep(i,n){
      if(i == k) continue;
      tmp = min(abs(x-v[i].fi),abs(y-v[i].se)); 
      if(tmp < dis){
        dis = tmp;
        g[k].push_back({i,dis});
        g[i].push_back({k,dis});
        cnt = 0;
      }
      else{
        ++cnt;
      }
      if(cnt > 10){
        break;
      }
    }
  }
  /* rep(i,V){                                              */
  /*   debug(i);                                            */
  /*   rep(j,g[i].size()){                                  */
  /*     cout << g[i][j].to << ' ' << g[i][j].cost << endl; */
  /*   }                                                    */
  /* }                                                      */
  std::cout << Prim() << std::endl;
  return 0;
}
