#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define REPL(i,m,n) for(ll i=(ll)(m); i<(ll)(n); i++)
#define repl(i,n) REPL(i,0,n)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

#define MAX_N 100000

class UnionFind
{
public:
  int par[MAX_N];
  int depth[MAX_N];
  int nGroup[MAX_N];

  UnionFind(int n) {
    init(n);
  }

  void init(int n) {
    for(int i=0; i<n; i++) {
      par[i] = i;
      depth[i] = 0;
      nGroup[i] = 1;
    }
  }

  int root(int x) {
    if(par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }

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

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) return;

    if(depth[x] < depth[y]) {
      par[x] = y;
      nGroup[y] += nGroup[x];
      nGroup[x] = 0;
    } else {
      par[y] = x;
      nGroup[x] += nGroup[y];
      nGroup[y] = 0;
      if(depth[x] == depth[y])
        depth[x]++;
    }
  }
};

struct edge {
  int u;
  int v;
  ll cost;
  edge(int _u, int _v, ll _cost) : u(_u), v(_v), cost(_cost) {}
};

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

vector<pair<int, int> > conn;
ll kruskal(int N) {
  sort(es.begin(), es.end(), comp);
  UnionFind uf(N);
  ll res = 0;
  rep(i, es.size()) {
    edge e = es[i];
    if(!uf.same(e.u, e.v)) {
      uf.unite(e.u, e.v);
      res += e.cost;
      conn.push_back(make_pair(e.u, e.v));
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<pair<ll, ll>> Porig(N);
  vector<pair<pair<ll, ll>, int>> P(N);
  rep(i, N) {
    cin >> Porig[i].first >> Porig[i].second;
    P[i].first.first = Porig[i].first;
    P[i].first.second = Porig[i].second;
    P[i].second = i;
  }
  sort(all(P));
  rep(i, N) {
    if(i) {
      es.push_back(edge(P[i].second,
                        P[i-1].second,
                        abs(P[i].first.first-P[i-1].first.first)));
    }
    if(i != N-1) {
      es.push_back(edge(P[i].second,
                        P[i+1].second,
                        abs(P[i].first.first-P[i+1].first.first)));
    }
  }
  sort(all(P), [&](pair<pair<ll, ll>, int> p1, pair<pair<ll, ll>, int> p2) {
      if(p1.first.second == p2.first.second) return p1.first.first < p2.first.first;
      else return p1.first.second < p2.first.second;
    });
  rep(i, N) {
    if(i) {
      es.push_back(edge(P[i].second,
                        P[i-1].second,
                        abs(P[i].first.second-P[i-1].first.second)));
    }
    if(i != N-1) {
      es.push_back(edge(P[i].second,
                        P[i+1].second,
                        abs(P[i].first.second-P[i+1].first.second)));
    }
  }
  kruskal(N);
  ll ans = 0;
  for(auto c: conn) {
    ans += min(abs(Porig[c.first].first-Porig[c.second].first),
               abs(Porig[c.first].second-Porig[c.second].second));
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 15208