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

#include<fstream>

#include<sstream>

#include<algorithm>

#include<cstdio>

#include<cassert>

#include<cmath>

#include<cstdlib>

#include<cstring>

#include<string>

#include<deque>

#include<queue>

#include<vector>

#include<list>

#include<map>

#include<set>

#include<bitset>

#include<stack>

#include<iomanip>

#include<utility>

#include<functional>

using namespace std;

int n,m;

int ans;

int par[100005];

vector<int> edge[100005];

vector<pair<int,int> > el;

pair<int,int> e[200005];

pair<pair<int,int>,int> a[100005],b[100005];

int find(int x)

{

	if(par[x]!=x)

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

	else

		return par[x];

}

bool unite(int x,int y)

{

	x=find(x);

	y=find(y);

	if(x==y)

		return false;

	else

	{

		par[x]=y;

		return true;

	}

}

int main()

{

	cin>>n;

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

	{

		cin>>a[i].first.first>>a[i].first.second;

		b[i].first.first=a[i].first.second;

		b[i].first.second=a[i].first.first;

		a[i].second=b[i].second=i;

	}

	sort(a,a+n);

	sort(b,b+n);

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

	{

		edge[a[i].second].push_back(a[i-1].second);

		e[m]=make_pair(a[i].second,a[i-1].second);

		el.push_back(make_pair(a[i].first.first-a[i-1].first.first,m++));

	}

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

	{

		edge[b[i].second].push_back(b[i-1].second);

		e[m]=make_pair(b[i].second,b[i-1].second);

		el.push_back(make_pair(b[i].first.first-b[i-1].first.first,m++));

	}

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

		par[i]=i;

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

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

	{

		int xx=e[el[i].second].first;

		int yy=e[el[i].second].second;

		if(unite(xx,yy))

			ans+=el[i].first;

	}

	cout<<ans;

	return 0;

}


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



using namespace std;

#define ll long long

#define f first

#define s second

ll n,x,y,tmp,ans;



int par[100005] , sz[100005];



int findPar(int node)

{

    if (par[node]==node) return node;

    return par[node] = findPar(par[node]);

}



void mergeCom(int node , int node2)

{

    int led = findPar(node) , led2 = findPar(node2);



    if (sz[led] > sz[led2])

    {

        par[led2]=led , sz[led] += sz[led2];

    }

    else

    {

        par[led]=led2 , sz[led2] += sz[led];

    }

}



int main()

{

    cin >> n;

    pair < pair<ll,ll> , ll> arr[n+1] , arr2[n+1];

    vector <pair<ll , pair<ll,ll> > > vec;



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

    {

        cin >> x >> y;

        arr[i].f.f=x , arr[i].f.s=y , arr[i].s=i;

        arr2[i].f.f=y , arr2[i].f.s=x , arr2[i].s=i;

        par[i]=i , sz[i]=1;

    }

    sort(arr+1,arr+n+1) , sort(arr2+1,arr2+n+1);

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

    {

        tmp = min(abs(arr[i+1].f.f-arr[i].f.f) , abs(arr[i+1].f.s-arr[i].f.s));

        vec.push_back({tmp , {arr[i+1].s , arr[i].s}});



        tmp = min(abs(arr2[i+1].f.f-arr2[i].f.f) , abs(arr2[i+1].f.s-arr2[i].f.s));

        vec.push_back({tmp , {arr2[i+1].s , arr2[i].s}});

    }



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

    tmp = n-1;

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

    {

        if (!tmp) break;

        x = vec[i].s.f , y = vec[i].s.s;

        if (findPar(x)==findPar(y)) continue;

        ans += vec[i].f , tmp--;

        mergeCom(vec[i].s.f , vec[i].s.s);

    }

    cout << ans << "\n";



    return 0;

}