#include <bits/stdc++.h>
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::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;
    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;
}
