// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



#define fi first

#define se second

#define pb push_back

#define mp make_pair



typedef pair<long long,long long> ii;

vector<pair<long long,ii>> edge;

long long n,up[100005],sz[100005];

pair<ii,long long> x[100005];



bool cmp(const pair<ii,long long> &a,const pair<ii,long long> &b){

    return a.fi.fi<b.fi.fi;

}

bool cmp1(const pair<ii,long long> &a,const pair<ii,long long> &b){

    return a.fi.se<b.fi.se;

}

int findSet(int x){

    if(x==up[x]) return x;

    else return findSet(up[x]);

}

void unionSet(int x,int y){

    long long x1=findSet(x),y1=findSet(y);

    if(sz[x1]<sz[y1]) swap(x1,y1);

    up[y1]=x1;

    sz[x1]+=sz[y1];

}

bool checkSet(int x,int y){

    long long x1=findSet(x),x2=findSet(y);

    if(x1==x2)

        return 1;

    else

        return 0;

}

int main(){

    cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);

    cin>>n;

    for(int i=1;i<=n;i++){

        cin>>x[i].fi.fi>>x[i].fi.se;

        x[i].se=i;

    }

    sort(x+1,x+1+n,cmp);

    for(int i=1;i<=n;i++){

        up[i]=i;

        sz[i]=1;

    }

    for(int i=1;i<n;i++){

        long long x1=x[i].se,x2=x[i+1].se;

        long long val=abs(x[i].fi.fi-x[i+1].fi.fi),val1=abs(x[i].fi.se-x[i+1].fi.se);

        edge.pb(mp(min(val,val1),mp(x1,x2)));

    }

    sort(x+1,x+1+n,cmp1);

    for(int i=1;i<n;i++){

        long long x1=x[i].se,x2=x[i+1].se;

        long long val=abs(x[i].fi.fi-x[i+1].fi.fi),val1=abs(x[i].fi.se-x[i+1].fi.se);

        edge.pb(mp(min(val,val1),mp(x1,x2)));

    }

    long long sum=0;

    sort(edge.begin(),edge.end());

    for(int i=0;i<edge.size();i++){

        long long x1=edge[i].se.fi,x2=edge[i].se.se;

        if(checkSet(x1,x2)==0){

            unionSet(x1,x2);

            sum+=edge[i].fi;

        }

    }

    cout<<sum;

}


// Faster C++ solution for the same problem (measured speedup: 5.237121078689264x):
#include <stdio.h>

#include <stdlib.h>

#include <algorithm>



// 全局变量

const int N = 100010 , M = N + N;

int x[N],y[N],id[N],n;



typedef struct ufset *UFset;

struct ufset

{

  int *parent;

  int *root;

};

UFset UFinit(int size)

{

  int e;

  UFset U=(UFset)malloc(sizeof *U);

  U->parent=(int*)malloc((size+1)*sizeof(int));

  U->root=(int*)malloc((size+1)*sizeof(int));

  for(e=1; e<=size; e++)

  {

    U->parent[e]=1;

    U->root[e]=1;

  }

  return U;

}

int UFfind(int e,UFset U)

{

  int i,j = e;

  while(!U->root[j]) j = U->parent[j];

  while(j!=e){

    i = U->parent[e];

    U->parent[e]=j;

    e=i;

  }

  return j;

}

int UFunion(int i,int j,UFset U)

{

  if(U->parent[i]<U->parent[j])

  {

    U->parent[j]+=U->parent[i];

    U->root[i]=0;

    U->parent[i]=j;

    return j;

  }

  else

  {

    U->parent[i]+=U->parent[j];

    U->root[j]=0;

    U->parent[j]=i;

    return i;

  }

}



// 记录优化后的边集数

struct Edge

{

  int u, v, cost;

  bool operator <(const Edge&b) const

  {

    return cost < b.cost;

  }

} edge[M];

int tot;

// 按照X轴排序

bool cmpX(int a, int b)

{

  return x[a] < x[b];

}

// 按照Y轴排序

bool cmpY(int a, int b)

{

  return y[a] < y[b];

}

void add(int c, int a, int b)

{

  edge[tot].cost = c;

  edge[tot].u = a;

  edge[tot++].v = b;

}

int main()

{

  scanf("%d", &n);

  int i;

  for (i = 1; i <= n; ++i)

  {

    scanf("%d%d", x + i, y + i);

    id[i] = i;

  }

  std::sort(id + 1 , id + n + 1, cmpX);

  for (i = 2; i <= n; ++i)

    add(x[id[i]] - x[id[i - 1]], id[i], id[i - 1]);

  std::sort(id + 1 , id + n + 1, cmpY);

  for (i = 2; i <= n; ++i)

    add(y[id[i]] - y[id[i - 1]], id[i], id[i - 1]);

  UFset UFS = UFinit(n);



  // 按照花费排序

  std::sort(edge, edge + tot);

  long long totcost = 0;

  for (i = 0; i < tot; ++i)

  {

    int u = edge[i].u, v = edge[i].v;

    u = UFfind(u,UFS), v = UFfind(v,UFS);

    if (u != v)

    {

      UFunion(u,v,UFS);

      totcost += edge[i].cost;

    }

  }

  printf("%lld\n", totcost);

  return 0;

}