#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstdlib>
#include <stack>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000009
# define FOR(i,a,n) for(register int i=a; i<=n; ++i)
# define FDR(i,a,n) for(register int i=a; i>=n; --i)
typedef long long ll;

using namespace std;
struct A
{
    int l;
    int r;
    int q;
};
A aa[100005];
int d[100005][100005];
int a[100005], b[100005];
int f[100005];
int finds(int x)
{
    return f[x] == x?x:f[x] = finds(f[x]);
}
int cmp(A a, A b)
{
    return a.q < b.q;
}
int main()
{
    int n;
    scanf("%d", &n);
    FOR(i,1,n)
    {
        scanf("%d%d", &a[i], &b[i]);
    }
    int ji = 0;
    FOR(i,1,n)
    {
        FOR(j,i+1,n)
        {
            aa[ji].l = i;
            aa[ji].r = j;
            aa[ji++].q = min(abs(a[i]-a[j]), abs(b[i]-b[j]));
        }
    }
    sort(aa, aa+ji, cmp);
    FOR(i,1,n)
    f[i] = i;
    int ans = 0;
    for(int i = 0; i < ji; i++)
    {
        int x = finds(aa[i].l);
        int y = finds(aa[i].r);
        if(x != y)
        {
            f[x] = y;
            ans += aa[i].q;
        }
    }
    printf("%d\n", ans);
    return 0;
}