#include <bits/stdc++.h>

using namespace std;

#define DUMP(x) std::cerr << (#x) << " = " << (x) << "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define ALL(r) r.begin(), r.end()
#define YES puts("YES")
#define Yes puts("Yes")
#define NO puts("NO")
#define No puts("No")
#define IMP puts("IMPOSSIBLE")
#define Imp puts("Impossible")
#define imp puts("impossible")
#define M1 puts("-1")
#define pb push_back

template <typename T>
ostream &operator<<(ostream &os, vector<T> &v)
{
    os << "{";
    rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", ");
    os << "}";
    return os;
}
template <typename T>
T dup(T x, T y)
{
    return (x + y - 1) / y;
};
template <typename A, size_t N, typename T>
inline void arrayFill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }
struct in
{
    const size_t n = 0;
    in() = default;
    in(size_t n) : n(n){};
    template <typename T>
    operator T()
    {
        T ret;
        cin >> ret;
        return ret;
    }
    template <typename T>
    operator vector<T>()
    {
        assert(n != 0);
        vector<T> ret(n);
        for (auto &x : ret)
            cin >> x;
        return ret;
    }
};

using ll = int64_t;
using vint = vector<int32_t>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pint = pair<int32_t, int32_t>;
using pll = pair<ll, ll>;
using setint = set<int32_t>;
using setstr = set<string>;
using qint = queue<int32_t>;
using qpint = queue<pint>;

constexpr std::int32_t INF = 1001001001;
constexpr std::int64_t LINF = 1001001001001001001;

struct Edge
{
    int from;
    int to;
    int cost;
};

class UnionFind
{
public:
    UnionFind(int n)
        : m_n(n), m_rank(m_n, 0)
    {
        m_par.resize(m_n);
        rep(i, m_n)
        {
            m_par[i] = i;
        }
    }

    int find(int x)
    {
        if (m_par[x] == x)
        {
            return x;
        }
        else
        {
            return m_par[x] = find(m_par[x]);
        }
    }

    void unite(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x == y)
            return;
        if (m_rank[x] < m_rank[y])
        {
            m_par[x] = y;
        }
        else
        {
            m_par[y] = x;
            if (m_rank[x] == m_rank[y])
                ++m_rank[x];
        }
    }

    bool same(int x, int y)
    {
        return find(x) == find(y);
    }

private:
    int m_n;
    vint m_par;
    vint m_rank;
};

void Main()
{
    int n = in();
    vector<tuple<int, int, int>> xy(n);
    rep(i, n)
    {
        int x, y;
        cin >> x >> y;
        --x, --y;
        xy[i] = make_tuple(x, y, i);
    }
    auto xy1 = xy;
    auto xy2 = xy;
    sort(ALL(xy1));
    sort(ALL(xy2), [](auto lhs, auto rhs) { return get<1>(lhs) < get<1>(rhs); });

    vector<Edge> edge_list(n);
    rep(i, n - 1)
    {
        int u1 = get<2>(xy1[i]);
        int v1 = get<2>(xy1[i + 1]);

        int dx = get<0>(xy1[i + 1]) - get<0>(xy1[i]);

        edge_list.pb({u1, v1, dx});
        edge_list.pb({v1, u1, dx});

        int u2 = get<2>(xy2[i]);
        int v2 = get<2>(xy2[i + 1]);

        int dy = get<1>(xy2[i + 1]) - get<1>(xy2[i]);

        edge_list.pb({u2, v2, dy});
        edge_list.pb({v2, u2, dy});
    }

    sort(ALL(edge_list), [](auto lhs, auto rhs) { return lhs.cost < rhs.cost; });

    ll ans = 0;
    UnionFind uf(n);
    for (const auto &e : edge_list)
    {
        if (!uf.same(e.from, e.to))
        {
            uf.unite(e.from, e.to);
            ans += e.cost;
        }
    }

    cout << ans << endl;
}

int main()
{
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}


Measured memory_bytes: 15172