#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <climits>
#define bogo_sort   std::sort
#define bozo_sort   std::stable_sort
#define alles(obj)  obj.begin(), obj.end()
#define elif        else if
#define unless(flg) if(!(flg))
#define elless(flg) else if(!(flg))
#define echo        std::cout <<
#define read        std::cin >>
#define endl        std::endl
#define fin         << '\n'
#define bash        push_back
#define fish        push_front
#define makePair    std::make_pair
#define _           << ' ' <<
// type-define
#define Stack       std::stack
#define Queue       std::queue
#define Deque       std::deque
#define PQueue      std::priority_queue
#define Set         std::set
#define Vector      std::vector
#define Pair        std::pair
#define Map         std::map
#define HashMap     std::unordered_map
#define Greater     std::greater
using String  =     std::string;
using llong   =     long long;
using boolean =     bool;
using Pii     =     Pair<int, int>;
using Vi      =     Vector<int>;
using Vii     =     Vector<Pii>;
// utils
constexpr int   dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int   dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
constexpr int   INF  = 0x3f3f3f3f;
constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL;

namespace {
    template<class A, class B> A power ( llong x, llong n, llong mod ) {
        llong ans = 1;
        while ( n > 0 ) {
            if ( n & 1 ) ans = ( ans * x ) % mod;
            x = ( x * x ) % mod;
            n >>= 1;
        }
        return ans;
    }
    template<class A, class B> A power ( A x, B n ) { return power( x, n, 1000000007 ); }
    template<class A> A gcd ( A x, A y ) { return x % y ? gcd( y, x % y ) : y; }
    template<class A, class B> A lcm   ( A x,  B y ) { return ( x / gcd(x, y) * y ); }
    template<class A> inline A abs ( A n ) { return ( n < 0 ) ? -n : n; }
    template<class A, class B> inline bool chmax ( A &a, const B &b ) { return b > a ? a = b, true : false; }
    template<class A, class B> inline bool chmin ( A &a, const B &b ) { return b < a ? a = b, true : false; }
    inline boolean isMovable ( int x, int y, int w, int h ) {
        return ( x >= 0 && y >= 0 && x < w && y < h );
    }
}

namespace Rlyeh {

    struct UnionFind {

        Vi data;

        UnionFind(int n) {
            data.resize(n+5);
            for (int i = 0; i < n+5; i++) {
                data[i] = -1;
            }
        }

        boolean unite(int x, int y) {
            int rootX = getRoot(x);
            int rootY = getRoot(y);
            if ( rootX == rootY ) return false;
            if ( data[rootX] < data[rootY] ) std::swap(rootX, rootY);
            data[rootX] += data[rootY];
            data[rootY] = rootX;
            return true;
        }

        int getRoot(int i) {
            return ( data[i] < 0 ) ? i : ( data[i] = getRoot(data[i]) );
        }

        int getSize(int n) {
            return -data[getRoot(n)];
        }

        boolean isUnited(int x, int y) {
            return (getRoot(x) == getRoot(y));
        }
    };

    Vii X, Y;
    using Piii = Pair<int, Pii>;
    using Viii = Vector<Piii>;
    Viii viii;
    UnionFind unionFind(100100);

    signed call_of_Cthulhu( signed datum ) {

        int n;
        read n;
        for ( int i = 0; i < n; i++ ) {
            int x, y;
            read x >> y;
            X.bash(Pii(x, i));
            Y.bash(Pii(y, i));
        }
        bozo_sort(alles(X));
        bozo_sort(alles(Y));
        for ( int i = 1; i < n; i++ ) {
            Pii s = X[i-1];
            Pii t = X[i];
            viii.bash(makePair(t.first-s.first, makePair(s.second, t.second)));

            s = Y[i-1];
            t = Y[i];
            viii.bash(makePair(t.first-s.first, makePair(s.second, t.second)));
        }
        bozo_sort(alles(viii));
        int res = 0;
        for ( int i = 0; i < viii.size(); i++ ) {
            if ( unionFind.unite(viii[i].second.first, viii[i].second.second)){
                res += viii[i].first;
            }
        }

        echo res fin;

        return 0;
    }
}

signed main(){std::cin.tie(0); std::ios::sync_with_stdio(false); int main_result = Rlyeh::call_of_Cthulhu(114514); return 0;}


Measured memory_bytes: 7640