#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline T chmax(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T chmin(T& a, const T b) { return a = (a > b) ? b : a; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 1000000007; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;

class UnionFind {
public:
    vector <int> par;
    vector <int> siz;

    UnionFind(int sz_): par(sz_), siz(sz_, 1LL) {
        for (int i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(int sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (int i = 0; i < sz_; ++i) par[i] = i;
    }

    int root(int x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }

    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

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

    int size(int x) {
        return siz[root(x)];
    }
};


template<typename T>
T kruskal(vector<vector<pair<int, int>>> &graph, int N){
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> q;
  // to from cost
  REP(i, N) REP(j, graph[i].size()){
    q.push(make_pair(graph[i][j].second, make_pair(i, graph[i][j].first)));
  }
  UnionFind uf(N);
  T res = 0;
  while(!q.empty()){
    auto p = q.top(); q.pop();
    if(uf.unite(p.second.first, p.second.second)){
      res += p.first;
    }
  }
  return res;
}

signed main() {
  int N;
  cin >> N;
  
  // x y i
  vector<pair<pair<int, int>, int>> c(N);
  REP(i, N){
    int x, y;
    cin >> x >> y;
    x--; y--;
    c[i] = make_pair(make_pair(x, y), i);
  }

  vector<vector<pair<int, int>>> graph(N);

  sort(ALL(c));

  REP(i, N-1){
    int cost = abs(c[i].first.first-c[i+1].first.first);
    graph[c[i].second].emplace_back(c[i+1].second, cost);
  }

  sort(ALL(c),
      [](pair<pair<int, int>, int> const & l, pair<pair<int, int>, int> const &  r){
      return l.first.second < r.first.second;
      });

  REP(i, N-1){
    int cost = abs(c[i].first.second-c[i+1].first.second);
    graph[c[i].second].emplace_back(c[i+1].second, cost);
  }

  cout << kruskal<int>(graph, N) << endl;

  return 0;
}

Measured memory_bytes: 18800