// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;

const int maxN=1e5+5;



int fa[maxN];



struct node

{

    int x,y,id;

}a[maxN],b[maxN];



struct edge

{

    int f,t,w;

}e[maxN*2];



bool cmp1(node x1,node x2)

{

    return x1.x<x2.x;

}



bool cmp2(node x1,node x2)

{

    return x1.y<x2.y;

}



bool cmp3(edge x1,edge x2)

{

    return x1.w<x2.w;

}



int find(int x)

{

    if(fa[x]==-1)return x;

    return fa[x]=find(fa[x]);

}



int uni(int f,int t)

{

    f=find(f);t=find(t);

    if(f==t)return 0;

    fa[f]=t;return 1;

}



int main()

{

    int k,n,cnt=0,now=0;

    long long ans=0;

    memset(fa,-1,sizeof(fa));

    scanf("%d",&n);

    for(k=0;k<n;k++)

    {

        scanf("%d%d",&a[k].x,&a[k].y);

        a[k].id=k;b[k].id=k;

        b[k].x=a[k].x;

        b[k].y=a[k].y;

    }

    sort(a,a+n,cmp1);

    sort(b,b+n,cmp2);

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

    {

        e[cnt].f=a[k-1].id;

        e[cnt].t=a[k].id;

        e[cnt].w=a[k].x-a[k-1].x;

        cnt++;

    }

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

    {

        e[cnt].f=b[k-1].id;

        e[cnt].t=b[k].id;

        e[cnt].w=b[k].y-b[k-1].y;

        cnt++;

    }

    sort(e,e+cnt,cmp3);

    for(k=0;k<cnt;k++)

    {

        if(uni(e[k].f,e[k].t))

        {

            ans+=e[k].w;

            now++;

        }

        if(now==n-1)break;

    }

    cout<<ans;

    return 0;

}


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



using namespace std;



typedef long long ll;



const int N = 1e5 + 5;

int parent[N];

int rnk[N];



int getParent(int x)

{

  if(parent[x] == x)

    return x;

  return parent[x] = getParent(parent[x]);

}



void connect(int x, int y)

{

  x = getParent(x), y = getParent(y);

  if(x == y)

    return;

  if(rnk[x] <= rnk[y])

  {

    parent[x] = y;

    rnk[y] += rnk[x];

  }

  else

  {

    parent[y] = x;

    rnk[x] += rnk[y];

  }

}



bool isconnected(int x, int y)

{

  return getParent(x) == getParent(y);

}



int main()

{

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

 int n;

 cin >> n;

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

  parent[i] = i, rnk[i] = 1;

 vector<pair<int, int>> points(n);

 vector<pair<int, pair<int, int>>> dist;

 vector<pair<pair<int, int>, int>> sortx, sorty;

 for(auto &x: points)

 {

   cin >> x.first >> x.second;

 }

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

 {

   sortx.push_back({points[i], i});

   sorty.push_back({{points[i].second, points[i].first}, i});

 }

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

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

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

 {

   int dif = sortx[i].first.first - sortx[i - 1].first.first;

   dist.push_back({dif, {sortx[i].second, sortx[i - 1].second}});

   dif = sorty[i].first.first - sorty[i - 1].first.first;

   dist.push_back({dif, {sorty[i].second, sorty[i - 1].second}});

 }

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

 ll ans = 0;

 for(auto &x: dist)

 {

   int firstNode = x.second.first, secondNode = x.second.second;

   if(isconnected(firstNode, secondNode))

    continue;

   ans += x.first;

   connect(firstNode, secondNode);

 }

 cout << ans;

 return 0;

}