#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long int MOD = 1000000007;
inline void add(long long int& x, long long int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  if (x < 0) x += MOD;
}
inline long long int mul(long long int x, long long int y) {
  return ((long long int)x * y) % MOD;
}
long long int gcd(long long int a, long long int b) {
  if (b) return gcd(b, a % b);
  return a;
}
long long int power(long long int a, long long int p) {
  long long int ret = 1;
  while (p) {
    if (p & 1) ret = mul(ret, a);
    a = mul(a, a);
    p /= 2;
  }
  return ret;
}
long long int phi(long long int n) {
  long long int ret = n;
  long long int i = 2;
  if (n % i == 0) {
    ret -= ret / i;
    while (n % i == 0) n /= i;
  }
  for (i = 3; i * i <= n; i++)
    if (n % i == 0) {
      ret -= ret / i;
      while (n % i == 0) n /= i;
    }
  if (n > 1) ret -= ret / n;
  return ret;
}
long long int n;
long long int dsu[100005], size[100005];
long long int root(long long int i) {
  while (dsu[i] != i) {
    dsu[i] = dsu[dsu[i]];
    i = dsu[i];
  }
  return i;
}
void weighted_union(long long int A, long long int B) {
  long long int root_A = root(A);
  long long int root_B = root(B);
  if (size[root_A] < size[root_B]) {
    dsu[root_A] = dsu[root_B];
    size[root_B] += size[root_A];
  } else {
    dsu[root_B] = dsu[root_A];
    size[root_A] += size[root_B];
  }
  return;
}
int main() {
  cin >> n;
  pair<long long int, long long int> a[100005];
  set<pair<long long int, long long int> > aa;
  for (long long int i = 0; i < n; i++) {
    ;
    scanf("%lld", &a[i].first);
    ;
    ;
    scanf("%lld", &a[i].second);
    ;
    aa.insert(make_pair(a[i].first, a[i].second));
  }
  long long int ct = 0;
  vector<pair<long long int, long long int> > xv, yv;
  for (__typeof(aa.begin()) i = (aa).begin(); i != (aa).end(); i++) {
    long long int x = (*i).first, y = (*i).second;
    xv.push_back(make_pair(x, ct));
    yv.push_back(make_pair(y, ct));
    ct++;
  }
  for (long long int i = 0; i < ct; i++) {
    dsu[i] = i;
    size[i] = 1;
  }
  sort(xv.begin(), xv.end());
  sort(yv.begin(), yv.end());
  set<pair<long long int, pair<long long int, long long int> > > dv;
  for (__typeof(xv.begin()) i = (xv).begin(); i != (xv).end(); i++) {
    long long int x1 = (*i).first, num = (*i).second;
    auto nx = next(i, 1);
    if (nx == xv.end()) continue;
    long long int x2 = (*nx).first, num2 = (*nx).second;
    dv.insert(make_pair(x2 - x1, make_pair(min(num, num2), max(num, num2))));
  }
  for (__typeof(xv.begin()) i = (xv).begin(); i != (xv).end(); i++) {
    long long int x1 = (*i).first, num = (*i).second;
    auto nx = next(i, 1);
    if (nx == xv.end()) continue;
    long long int x2 = (*nx).first, num2 = (*nx).second;
    dv.insert(make_pair(x2 - x1, make_pair(min(num, num2), max(num, num2))));
  }
  for (__typeof(yv.begin()) i = (yv).begin(); i != (yv).end(); i++) {
    long long int x1 = (*i).first, num = (*i).second;
    auto nx = next(i, 1);
    if (nx == yv.end()) continue;
    long long int x2 = (*nx).first, num2 = (*nx).second;
    dv.insert(make_pair(x2 - x1, make_pair(min(num, num2), max(num, num2))));
  }
  long long int ans = 0;
  for (__typeof(dv.begin()) i = (dv).begin(); i != (dv).end(); i++) {
    long long int d = (*i).first, v1 = (*i).second.first,
                  v2 = (*i).second.second;
    if (root(v1) != root(v2)) {
      weighted_union(v1, v2);
      ans += d;
    }
    if (size[root(0)] == ct - 1) break;
  }
  cout << ans << endl;
  return 0;
}
