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



using namespace std;



struct point

{

	int x, y, id;

} p[100005];



vector< pair<int, pair<int, int> > > q;



bool cmpx(point a, point b)

{

	if (a.x != b.x) return a.x > b.x;

	else return a.y > b.y;

}



bool cmpy(point a, point b)

{

	if (a.y != b.y) return a.y > b.y;

	else return a.x > b.x;

}



int up[100005];



void makesets()

{

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

	{

		up[i] = -1;

	}

}



int Find(int x)

{

	if (up[x] < 0) return x;

	int root = Find(up[x]);

	up[x] = root;

	return root;

}



void Union(int x, int y)

{

	if (Find(x) == Find(y)) return;

	if (up[x] < up[y])

	{

		up[x] += up[y];

		up[y] = x;

	}

	else

	{

		up[y] += up[x];

		up[x] = y;

	}

}



int n;



signed main()

{

	scanf("%d", &n);

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

	{

		scanf("%d%d", &p[i].x, &p[i].y);

		p[i].id = i;

	}

	sort(p+1, p+1+n, cmpx);

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

	{

		q.push_back(make_pair(p[i].x - p[i+1].x, make_pair(p[i].id, p[i+1].id)));

	}

	sort(p+1, p+1+n, cmpy);

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

	{

		q.push_back(make_pair(p[i].y - p[i+1].y, make_pair(p[i].id, p[i+1].id)));

	}

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

	makesets();

	long long res = 0;

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

	{

		int u = q[i].second.first, v = q[i].second.second;

		if (Find(u) != Find(v))

		{

			Union(Find(u), Find(v));

			res += q[i].first;

		}

	}

	printf("%lld", res);

}

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

#define int long long

using namespace std;

typedef pair<int,int>ii;

typedef vector<int>vi;

typedef pair<int,ii>iii;

const int N=1e5+5;

vector<ii>a,b;

map<ii,int>check;

vi pset,s;

bool cmp(ii a,ii b )

{

    return a.second<b.second;

}

void init(int n)

{

    pset.assign(n+1,-1);

    s.assign(n+1,1);

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

        pset[i]=i;

    }

}

int findset(int i)

{

    if(pset[i]==i)return i;

    else return pset[i]=findset(pset[i]);

}

void unionset(int i,int j)

{

    int x=findset(i),y=findset(j);

    if(s[x]>s[y]){

        pset[y]=x;

        s[x]+=s[y];

    }

    else{

        pset[x]=y;

        s[y]+=s[x];

    }

}

signed main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

    int n,cnt=0,ans=0;

    cin>>n;

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

        int x,y;

        cin>>x>>y;

        if(check[ii(x,y)])continue;

        else{

            a.push_back(ii(x,y));

            b.push_back(ii(x,y));

            check[ii(x,y)]=++cnt;

        }

    }

    init(a.size()+1);

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

    sort(b.begin(),b.end(),cmp);

    priority_queue<iii,vector<iii>,greater<iii> >q;

    //for(int i=1;i<=a.size();i++)cout<<pset[i]<<endl;

    for(int i=0;i<a.size()-1;i++){

        int x=a[i].first,y=a[i].second;

        q.push(iii(a[i+1].first-x,ii(check[a[i]],check[a[i+1]])));

    }

    for(int i=0;i<a.size()-1;i++){

        int x=b[i].first,y=b[i].second;

        q.push(iii(b[i+1].second-y,ii(check[b[i]],check[b[i+1]])));

    }

    while(!q.empty()){

        int c=q.top().first,u=q.top().second.first,v=q.top().second.second;

        q.pop();

        if(findset(u)!=findset(v)){

            ans+=c;

            unionset(u,v);

        }

    }

    cout<<ans;

}