#include<bits/stdc++.h>
using namespace std;
namespace Flandle_Scarlet
{
    #define ll long long
    #define N 200100
    class Graph
    {
        public:
            int head[N];
            int EdgeCount;
            struct Edge
            {
                int To,Label,Next;
            }Ed[N<<1];

            void clear()
            {
                memset(Ed,-1,sizeof(Ed));
                memset(head,-1,sizeof(head));
                EdgeCount=0;
            }
            void AddEdge(int u,int v,int w)
            {
                ++EdgeCount;
                Ed[EdgeCount]=(Edge){v,w,head[u]};
                head[u]=EdgeCount;
            }

            int Start(int u)
            {
                return head[u];
            }
            int To(int u)
            {
                return Ed[u].To;
            }
            int Label(int u)
            {
                return Ed[u].Label;
            }
            int Next(int u)
            {
                return Ed[u].Next;
            }
    }G;void Add(int u,int v,int w){G.AddEdge(u,v,w);G.AddEdge(v,u,w);}
    struct point
    {
        int x,y,id;
    }a[N];
    int operator^(point x,point y)
    {
        return min
        (
            abs(x.x-y.x)
            ,abs(x.y-y.y)
        );
    }
    bool cmpx(point x,point y){return x.x<y.x;}
    bool cmpy(point x,point y){return x.y<y.y;}
    bool cmpid(point x,point y){return x.id<y.id;}


    struct Edge
    {
        int u,v,w;
    }E[N];int Ecnt=0;
    bool operator<(Edge &a,Edge &b){return a.w<b.w;}
    void AddEd(int u,int v,int w){E[++Ecnt]=(Edge){u,v,w};}

    int n;
    void Input()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[i]=(point){x,y,i};
        }
    }

    class DSU
    {
        public:
            int Father[N];
            void Init()
            {
                for(int i=0;i<N;++i)
                {
                    Father[i]=i;
                }
            }
            int Find(int x)
            {
                return x==Father[x]?x:Father[x]=Find(Father[x]);
            }
            void Merge(int x,int y)
            {
                int ax=Find(x),ay=Find(y);
                Father[ay]=ax;
            }
    }D;
    ll Kruskal()
    {
        D.Init();
        sort(E+1,E+Ecnt+1);
        ll ans=0;int cnt=0;
        for(int i=1;i<=Ecnt;++i)
        {
            int u=E[i].u,v=E[i].v,w=E[i].w;
            if (D.Find(u)!=D.Find(v))
            {
                D.Merge(u,v);
                ans+=1ll*w;
                if (++cnt==n-1) break;
            }
        }
        return ans;
    }
    void BuildGraph()
    {
        G.clear();
        Ecnt=0;
        sort(a+1,a+n+1,cmpx);
        for(int i=2;i<=n;++i)
        {
            int dis=a[i].x-a[i-1].x;
            Add(a[i].id,a[i-1].id,dis);
            AddEd(a[i].id,a[i-1].id,dis);
        }
        sort(a+1,a+n+1,cmpy);
        for(int i=2;i<=n;++i)
        {
            int dis=a[i].y-a[i-1].y;
            Add(a[i].id,a[i-1].id,dis);
            AddEd(a[i].id,a[i-1].id,dis);
        }
        sort(a+1,a+n+1,cmpid);
    }
    void Soviet()
    {
        BuildGraph();
        printf("%lld\n",Kruskal());
    }
    void IsMyWife()
    {
        if (0)
        {
            freopen("","r",stdin);
            freopen("","w",stdout);
        }
        Input();
        Soviet();
    }
};
int main()
{
    Flandle_Scarlet::IsMyWife();
    return 0;
}


Measured memory_bytes: 11136