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

using namespace std;

int n,x,y,k;

long long ans=0;

const int inf = 0x3f3f3f3f;

#define ft first

#define se second

const int maxn=100007;

pair <int,int> r[maxn];

pair <int,int> c[maxn];

struct Node{

	int u,v,cst;

}ege[2*maxn+1000];

Node make_node(int a,int b,int c){

	Node node;

	node.u=a;

	node.v=b;

	node.cst=c;

	return node;

}

int f[maxn],rnk[maxn];

int fnd(int x){

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

	return f[x]=fnd(f[x]);

}

void merge(int x,int y){

	x=fnd(x);

	y=fnd(y);

	if (rnk[x]>rnk[y])f[y]=x;

	else {

		f[x]=y;

		if (rnk[x]==rnk[y])rnk[y]++;

	}

}

bool cmp(Node a,Node b){

	return a.cst<b.cst;

}

void K(){

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

		if (fnd(ege[i].u)==fnd(ege[i].v))continue;

		ans+=ege[i].cst;

		merge(ege[i].u,ege[i].v);

//		printf("%lld\n",ans);

	}

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

}

int main(){

	scanf("%d",&n);

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

		scanf("%d%d",&x,&y);

		r[i]=make_pair(x,i);

		c[i]=make_pair(y,i);

	}

	sort(r,r+n);

	sort(c,c+n);

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

		ege[++k]=make_node(r[i-1].se,r[i].se,abs(r[i].ft-r[i-1].ft));

		ege[++k]=make_node(c[i-1].se,c[i].se,abs(c[i].ft-c[i-1].ft));

	}

	sort(ege+1,ege+1+k,cmp);

	memset(f,inf,sizeof f);

	for (int i=0;i<n;++i)f[i]=i,rnk[i]=1;

	K();

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 7.209959002564859x):
#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;

}