#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <cmath>
#include <string>
#include <sstream>
#include <queue> 
#include <list>
#include <stack>
#include <unordered_map>
#include <algorithm> 
#include <numeric>  
#include <functional>
#include <cassert>
#include <bitset>
#include <cctype>  
#include <iomanip> 
#include <limits>  
#include <regex>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt, less) : 
// if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*")) ) std::cout << "YES" << std::endl;
 
#define rep(i, n) for(int i = 0; i < (int)(n);    i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
typedef std::pair<int,int> P;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;

ll gcd ( ll a, ll b ) { if ( b == 0 ) return a; else return gcd ( b, a%b ); }
ll lcm ( ll a, ll b ) { return a * b / gcd(a, b); }

template <class T> class UnionFind {
  private:
    struct subset  { 
      int parent; 
      int rank;
      T diff_weight;
      int sizes;
    }; 
    std::vector<subset> subsets;

  public:
    UnionFind (int N) {
      subsets.resize(N);
      for(int i=0; i<N; i++) subsets[i] = subset( { i, 0, T(0), 1 } );
    }

    int Find (int x) { 
      if (subsets[x].parent == x) return x;
      int r = Find(subsets[x].parent); 
      subsets[x].diff_weight += subsets[subsets[x].parent].diff_weight;
      return subsets[x].parent = r; 
    } 

    T weight(int x) {
      Find(x);
      return subsets[x].diff_weight;
    }

    bool Same (int x, int y) {
        return Find(x) == Find(y);
    }

    bool Union (int x, int y, T w) {
      w += weight(x); w -= weight(y);
      x = Find(x);  y = Find(y); 
      if ( x == y ) return false;

      if (subsets[x].rank < subsets[y].rank) {
        std::swap(x, y);
        w = -w;
      }
      if (subsets[x].rank == subsets[y].rank) {
        subsets[x].rank++; 
        subsets[x].sizes += subsets[y].sizes;
      }
      subsets[y].parent = x;
      subsets[y].diff_weight = w;
      return true;
    }

    T diff(int x, int y) {
      assert( Same(x,y) );
      return weight(y) - weight(x);
    }

    int size (int x) {
      return subsets[Find(x)].sizes;
    }
};


int main (void) {
  std::cin.tie(0);  std::ios::sync_with_stdio(false);
  int N; std::cin >> N;
  std::vector<int> x(N), y(N);
  rep(i,N) std::cin >> x[i] >> y[i];

  struct Edge{
    ll cost;
    int a;
    int b;
  };
  std::vector<Edge> edges;

  std::vector<int> ids(N); rep(i,N) ids[i] = i;

  // x
  std::sort( all(ids), [&](int i, int j) { return x[i] < x[j]; } );
  for(int i=0; i<N-1; i++) {
    auto u = ids[i];
    auto v = ids[i+1];
    edges.push_back( Edge( { std::abs(x[u]-x[v]), u, v } ) );
  }

  // y
  rep(i,N) ids[i] = i;
  std::sort( all(ids), [&](int i, int j) { return y[i] < y[j]; } );
  for(int i=0; i<N-1; i++) {
    auto u = ids[i];
    auto v = ids[i+1];
    edges.push_back( Edge( { std::abs(y[u]-y[v]), u, v } ) );
  }

  // Kruskal
  std::sort( all(edges), [](Edge &x, Edge &y) { return x.cost < y.cost; } );
  UnionFind<int> uf(N);
  ll ans = 0;
  for(auto&& e : edges) {
    if ( uf.Same(e.a,e.b) ) continue;
    uf.Union(e.a,e.b,0);
    ans += e.cost;
  }
  std::cout << ans << std::endl;

  return 0;
}


Measured memory_bytes: 7664