#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
using namespace std;
#define i64  long long
#define int  long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
#define MEM_SIZE 10000

// int DP[MEM_SIZE][MEM_SIZE] = {0};
// int GMEM[MEM_SIZE][MEM_SIZE] = {0};
template<typename T> void DEBUG(T e){std::cout << e << std::endl;}
template<class T> void DEBUG(string str, T e){std::cout <<str << ">>" << e << std::endl;}
template<typename T> void DEBUG(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ DEBUG(v); } }
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

struct place{int i,x,y;};
struct edge{int u,v,cost;};

int par[200000]; //union-find
int ranks[200000]; //union-find
void init(int n){ //union-find 
    for(int i=0;i<n;i++){
        par[i]=i;
        ranks[i]=0;
    }
}
 
int find(int x){ //union-find
    if(par[x]==x){
        return x;
    }
    else{
        return par[x]=find(par[x]);
    }
}
 
void unite(int x, int y){ //union-find
    x=find(x);
    y=find(y);
    if(x==y) return;
    if(ranks[x]<ranks[y]){
        par[x]=y;
    }
    else{
        par[y]=x;
        if(ranks[x]==ranks[y]) ranks[x]++;
    }
}
 
bool same(int x, int y){ //union-find
    return find(x) == find(y);
}

bool comp(const edge & e1,const edge& e2)
{
  return e1.cost<e2.cost;
}
bool sx(const place &a,const place &b)
{
  return a.x < b.x;
}

bool sy(const place &a,const place &b)
{
  return a.y < b.y;
}




void solve(void)
{
  int N;
  cin>>N;
  vector<place> V;
  vector<edge>G;
  vector<int>X(N),Y(N);
  for (int i = 0; i < N; i++)
  {
    int xx, yy;
    cin>>xx>>yy;
    place p = {i,xx,yy};
    V.emplace_back(p);
  }
  sort(V.begin(),V.end(),sx);

  for (int i = 0; i < N-1; i++)
  {
    edge  e = {V[i].i,V[i+1].i,V[i+1].x - V[i].x}; 
    G.push_back(e);
  }
  
    sort(V.begin(),V.end(),sy);

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

    edge  e = {V[i].i,V[i+1].i,V[i+1].y - V[i].y}; 
    G.push_back(e);
  }
  init(N);
  sort(G.begin(),G.end(),comp);

  int res = 0;
  for (int i = 0; i < G.size(); i++)
  {
    edge e = G[i];
    if(!same(e.u,e.v))
    {
      unite(e.u,e.v);
      res+=e.cost;
      // DEBUG(e.cost);
    }
  }
  cout<<res<<endl;
  
  
  
  
  return; 
}


int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(9);
  solve();

 
  return 0;
}

Measured memory_bytes: 12524