#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000000;
struct Town {
  Town(int i_, int x_, int y_) : i{i_}, x{x_}, y{y_} {}
  int i;
  int x;
  int y;
};
using Towns = vector<Town>;
using TownMap = map<int, vector<int>>;
tuple<int, int, int> find_min_path(const TownMap& map) {
  int min_value = MAX;
  int town1 = -1, town2 = -1;
  auto prev = map.end();
  for (auto itor = map.begin(); itor != map.end(); ++itor) {
    if (prev != map.end()) {
      int d = itor->first - prev->first;
      if (d < min_value) {
        min_value = d;
        town1 = prev->second[0];
        town2 = itor->second[0];
      }
    }
    prev = itor;
  }
  return make_tuple(min_value, town1, town2);
}
tuple<int, int> find_min_path(const vector<bool>& visited, const TownMap& xs,
                              const TownMap& ys, const Town& town) {
  int next_i = -1, next_cost = MAX;
  for (auto i = xs.find(town.x); i != xs.end(); ++i) {
    for (const auto ti : i->second) {
      if (!visited[ti]) {
        int d = i->first - town.x;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }
  for (auto i = xs.find(town.x); i != xs.begin();) {
    --i;
    for (const auto ti : i->second) {
      if (!visited[ti]) {
        int d = town.x - i->first;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }
  for (auto i = ys.find(town.y); i != ys.end(); ++i) {
    for (const auto ti : i->second) {
      if (!visited[ti]) {
        int d = i->first - town.y;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }
  for (auto i = ys.find(town.y); i != ys.begin();) {
    --i;
    for (const auto ti : i->second) {
      if (!visited[ti]) {
        int d = town.y - i->first;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }
  return make_tuple(next_cost, next_i);
}
tuple<int, int> find_next_path(const Towns& towns, const vector<int>& vtowns,
                               const vector<bool>& visited, const TownMap& xs,
                               const TownMap& ys) {
  int min_cost = MAX, min_town = -1;
  for (const auto& i : vtowns) {
    int cost, town;
    tie(cost, town) = find_min_path(visited, xs, ys, towns[i]);
    if (min_town == -1 or cost < min_cost) {
      min_cost = cost;
      min_town = town;
    }
  }
  return make_tuple(min_cost, min_town);
}
long build(const Towns& towns) {
  map<int, vector<int>> xs, ys;
  for (const auto& town : towns) {
    xs[town.x].push_back(town.i);
    ys[town.y].push_back(town.i);
  }
  vector<bool> visited(towns.size(), false);
  vector<int> vtowns;
  vtowns.reserve(towns.size());
  int x_cost, x1, x2;
  int y_cost, y1, y2;
  tie(x_cost, x1, x2) = find_min_path(xs);
  tie(y_cost, y1, y2) = find_min_path(ys);
  long costs = 0;
  if (x_cost < y_cost) {
    vtowns.push_back(x1);
    vtowns.push_back(x2);
    visited[x1] = visited[x2] = true;
    costs += x_cost;
  } else {
    vtowns.push_back(y1);
    vtowns.push_back(y2);
    visited[y1] = visited[y2] = true;
    costs += y_cost;
  }
  while (vtowns.size() < towns.size()) {
    int town, cost;
    tie(cost, town) = find_next_path(towns, vtowns, visited, xs, ys);
    vtowns.push_back(town);
    visited[town] = true;
    costs += cost;
  }
  return costs;
}
int main() {
  int N;
  cin >> N;
  Towns towns;
  towns.reserve(N);
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    towns.push_back(Town(i, x, y));
  }
  cout << build(towns) << endl;
  return 0;
}
