#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <deque>
#include <unordered_map>
#include <unordered_set>

using pair_t = std::pair<int, int>;

int main(void)
{
  int N;
  std::cin >> N;
  std::vector<pair_t> pos(N);

  for(int i = 0; i < N; ++i){
    std::cin >> pos[i].first >> pos[i].second;
  }
  std::sort(pos.begin(), pos.end(), [](auto lhs, auto rhs){ return lhs.first != rhs.first ? lhs.first < rhs.first : lhs.second < rhs.second; });
  auto new_end = std::unique(pos.begin(), pos.end());
  N = new_end - pos.begin();
  pos.erase(new_end, pos.end());

  if(N == 0){ std::cout << "0" << std::endl; return 0; }

  std::vector<std::vector<int> > ds(N, std::vector<int>(N, 0));

  for(int i = 0; i < N; ++i){
    for(int j = 0; j < N; ++j){
      int d = std::min(std::abs(pos[i].first-pos[j].first), std::abs(pos[i].second-pos[j].second));
      ds[i][j] = d;
    }
  }

  int ans = 0;
  std::vector<bool> isCon(N, false);

  int min = ds[0][1];
  int start = 0, end = 1;
  for(int i = 0; i < N; ++i){
    for(int j = 0; j < N; ++j){
      if(i == j){ continue; }
      if(min > ds[i][j]){
	min = ds[i][j];
	start = i;
	end = j;
      }
    }
  }
  isCon[start] = true;
  isCon[end] = true;
  ans += min;

  while(std::find(isCon.begin(), isCon.end(), false) != isCon.end()){
    using searchee_t = std::pair<int, pair_t>;
    std::vector<searchee_t>  searchee; // cost, pos
    for(int i = 0; i < N; ++i){
      for(int j = 0; j < N; ++j){
	if(i == j){ continue; }
	if(isCon[i] ^ isCon[j] == false){ continue; }
	searchee.push_back(std::make_pair(ds[i][j], std::make_pair(i,j)));
      }
    }
    auto min_elem = std::min_element(searchee.begin(), searchee.end(), [](searchee_t const& lhs, searchee_t const& rhs){ return lhs.first < rhs.first; });
    ans += min_elem->first;
    isCon[min_elem->second.first] = true;
    isCon[min_elem->second.second] = true;
  }
  std::cout << ans << std::endl;
  return 0;
}
