#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/detail/standard_policies.hpp>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)

#define dbg(x) cerr << (#x) << " --> " << (x) << nl;
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

#define Toktama ""

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef tree < pair <int, int>, null_type, less < pair <int, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 1e5 + 7, inf = 1e9 + 7, mod = 1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

int get_int() {
  char x = getchar();
  bool mns = 0;
  while (!isdigit(x)) mns |= x == '-', x = getchar();
  int res = 0;
  while (isdigit(x)) res = res * 10 + x - '0', x = getchar();
  if (mns) res = -res;
  return res;
}
void add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
}
int mult(int x, int y) {
  return x * 1ll * y % mod;
}
int sum(int x, int y) {
  add(x, y);
  return x;
}

int n;
int p[N], lvl[N];
vector < pair <int, pair <int, int> > > g;
struct node {
  int v, u, cost;
  node(int v, int u, int cost) : v(v), u(u), cost(cost) {}
  bool operator < (const node &x) {
    return cost < x.cost;
  }
};
int get(int x) {
  if (p[x] == x) return x;
  return p[x] = get(p[x]);
}
bool unit(int x, int y) {
  int px = get(x), py = get(y);
  if (px == py) return 0;
  if (lvl[px] > lvl[py]) p[py] = px;
  else if (lvl[py] > lvl[px]) p[px] = py;
  else p[px] = py, lvl[px]++;
  return 1;
}
bool cmpx(pair <int, pair <int, int> > l, pair <int, pair <int, int> > r) {
  return l.s.f < r.s.f;
}
bool cmpy(pair <int, pair <int, int> > l, pair <int, pair <int, int> > r) {
  return l.s.s < r.s.s;
}
void solve() {
  n = get_int();
  rep(i, 1, n) {
    int x = get_int(), y = get_int();
    g.pb({0, {x, y}});
  }
  sort(all(g));
  g.erase(unique(all(g)), g.end());
  n = sz(g) - 1;
  rep(i, 0, n)
    g[i].f = i, p[i] = i, lvl[i] = 1;
  sort(all(g), cmpx);
  vector <node> a;
  rep(i, 0, n) {
    rep(j, i + 1, min(n, i + 300))
      a.pb(node(g[i].f, g[j].f, min(abs(g[j].s.f - g[i].s.f), abs(g[j].s.s - g[i].s.s))));
  }
  sort(all(g), cmpy);
  rep(i, 0, n) {
    rep(j, i + 1, min(n, i + 300))
      a.pb(node(g[i].f, g[j].f, min(abs(g[j].s.f - g[i].s.f), abs(g[j].s.s - g[i].s.s))));
  }
  sort(all(a));
  ll ans = 0;
  for (auto i : a)
    if (unit(i.v, i.u)) ans += i.cost;
  cout << ans;
}
int main() {
  #ifndef Toktama
    freopen (Toktama".in", "r", stdin);
    freopen (Toktama".out", "w", stdout);
  #endif
  int t = 1;
  while (t--)
    solve();
  ioi
}
