#include<bits/stdc++.h>

using namespace std;
inline void read(long long &x) {
    char ch;
    bool flag = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true;
    for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}
inline void write(long long x) {
    static const long long maxlen = 100;
    static char s[maxlen];
    if (x < 0) {   putchar('-'); x = -x;}
    if (!x) { putchar('0'); return; }
    long long len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0';
    for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}

inline void read(int &x){
    char ch;
    bool flag = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true;
    for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}

const long long MAXN =1001000;

long long n;
struct kt{
long long x,y;
long long id;
}a[ MAXN ];

long long fa[ MAXN ];


struct edge{
long long x,y,val;
}E[ MAXN*2 ];

bool cmp_x(kt A,kt B){
return A.x<B.x;
}

bool cmp_y(kt A,kt B){
return A.y<B.y;
}

bool cmp_e(edge A,edge B){
return A.val<B.val;
}

long long get_fa(long long x){
return (fa[x]==x)?(x):(fa[x]=get_fa(fa[x]));
}


int main(){
    read(n);
    for (long long i=1;i<=n;i++)
    {
        a[i].id=i;
        read(a[i].x);read(a[i].y);
    }
    sort(a+1,a+n+1,cmp_x);
    long long cnt=0;
    for (long long i=1;i<n;i++)
        {
            ++cnt;
            E[cnt].x=a[i].id;
            E[cnt].y=a[i+1].id;
            E[cnt].val=a[i+1].x-a[i].x;
        }

    sort(a+1,a+n+1,cmp_y);
    for (long long i=1;i<n;i++)
        {
            ++cnt;
            E[cnt].x=a[i].id;
            E[cnt].y=a[i+1].id;
            E[cnt].val=a[i+1].y-a[i].y;
        }
    for (long long i=1;i<=n;i++)
        fa[i]=i;

    sort(E+1,E+cnt+1,cmp_e);
    long long ans=0;
    for (long long i=1;i<=cnt;i++)
        {
            long long a=E[i].x,b=E[i].y;
            long long x=get_fa(a),y=get_fa(b);
            if ( x!=y )
                {
                    ans+=E[i].val;
                    fa[x]=y;
                }
        }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vd=vector<double>;
using vvd=vector<vd>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using vs=vector<string>;
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define LINF    	((ll)1ll<<60)
#define INF     	((int)1<<30)
#define EPS     	(1e-9)
#define MOD     	(1000000007)
#define fcout(a)	cout<<setprecision(a)<<fixed
#define fs 			first
#define sc			second
#define PI			3.141592653589793
template<class S,class T>ostream& operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s; a=0;} return os;}
/*
 * UnionFind。O(ｱｯｶｰﾏﾝ)
 */
struct UnionFind{
	vi p;
	UnionFind(int s):p(s,-1){}
	void unite(int a,int b){
		a=root(a);b=root(b);
		if(a!=b){
			if(p[b]<p[a])swap(a,b);
			p[a]+=p[b];p[b]=a;
		}
	}
	bool same(int a,int b){return root(a)==root(b);}
	int root(int a){return p[a]<0?a:p[a]=root(p[a]);}
};

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
   	int n;
   	cin>>n;
   	using D=tuple<int,int,int>;
   	vector<D> data;
   	
   	rep(i,n){
   	    int x,y;
   	    cin>>x>>y;
   	    data.emplace_back(i,x,y);
   	}
   	vector<D> edge;
   	sort(all(data),[](D a,D b){return get<1>(a)<get<1>(b);});
   	rep(i,n-1){
   	    int a0,a1,a2,b0,b1,b2;
   	    tie(a0,a1,a2)=data[i];
   	    tie(b0,b1,b2)=data[i+1];
   	    edge.emplace_back(b1-a1,a0,b0);
   	}
   	sort(all(data),[](D a,D b){return get<2>(a)<get<2>(b);});
   	rep(i,n-1){
   	    int a0,a1,a2,b0,b1,b2;
   	    tie(a0,a1,a2)=data[i];
   	    tie(b0,b1,b2)=data[i+1];
   	    edge.emplace_back(b2-a2,a0,b0);
   	}
   	int ans=0;
   	UnionFind uf(n);
   	sort(all(edge),[](D a,D b){return get<0>(a)<get<0>(b);});
   	for(auto&e:edge){
   	    int c,a,b;
   	    tie(c,a,b)=e;
   	    if(uf.same(a,b))continue;
   	    uf.unite(a,b);
   	    ans+=c;
   	}
   	cout<<ans<<endl;
}
