#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;

//
#define int long long
#define m0(x) memset((x), 0, sizeof(x))
#define mm(x) memset((x), -1, sizeof(x))

//container
#define ALL(x) (x).begin(), (x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
#define PERM(c) \
  sort(ALL(c)); \
  for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))

// debug
#define GET_VAR_NAME(variable) #variable
#define test(x) cout << GET_VAR_NAME(x) << " = " << x << endl;

// bit_macro
#define bit(n) (1LL << (n))
#define bitset(a, b) (a) |= (1 << (b))
#define bitunset(a, b) (a) |= ~(1 << (b))
#define bitcheck(a, b) ((a) >> (b)) & 1
#define bitcount(a) __builtin_popcountll((a))

//typedef
typedef long long lint;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

//constant
const int INF = (int)1e18;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
const int dx[] = {-1, 0, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, 0, 1, -1, 1, -1};

//
template <typename T>
void chmax(T &a, T b) { a = max(a, b); }
template <typename T>
void chmin(T &a, T b) { a = min(a, b); }
//
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
struct Accelerate_Cin {
  Accelerate_Cin() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
};

const int MAX_E = 1010101;

//Ｏ(|E|log|V|)
struct edge {
  int u, v, cost;
}; //辺e(u,v)のコストcost

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

edge es[MAX_E];
int V, E; //頂点数、辺数

int par[MAX_E];
int rank_[MAX_E];

//ここからUnion-Find

// n要素で初期化
void init_union_find(int n) {
  for (int t = 0; t < n; t++) {
    par[t] = t;
    rank_[t] = 0;
  }
}

//木の根を求める。
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとｙの集合を併合。
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rank_[x] < rank_[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank_[x] == rank_[y])
      rank_[x]++;
  }
}

// xとｙが同じ集合に属するかどうか。
bool same(int x, int y) { return find(x) == find(y); }

//ここからKruskal

int kruskal() {
  sort(es, es + E, comp); // edge.costが小さい順にsortする
  init_union_find(V);     // Union-Findの初期化
  int res = 0;            // res:集合Ｘの辺の総和

  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

signed main() {
  cin >> V;
  E = 2 * V;

  map<P, int> mp;
  vector<P> X, Y;
  for (int i = 0; i < V; i++) {
    int x, y;
    cin >> x >> y;
    X.push_back(P(x, y));
    Y.push_back(P(y, x));
    mp[P(x, y)] = i;
  }

  sort(ALL(X));
  sort(ALL(Y));

  int t = 0;
  for (int i = 0; i < V - 1; i++) {
    es[t] = (edge){mp[X[i]], mp[X[i + 1]], abs(X[i].first - X[i + 1].first)};
    t++;
  }

  for (int i = 0; i < V - 1; i++) {
    P tmp1 = P(Y[i].second, Y[i].first);
    P tmp2 = P(Y[i + 1].second, Y[i + 1].first);

    es[t] = (edge{mp[tmp1], mp[tmp2], abs(tmp1.second - tmp2.second)});
    t++;
  }

  cout << kruskal() << endl;
  return 0;
}


Measured memory_bytes: 22752