#include <algorithm>
#include <bits/stdc++.h>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <queue>
#include <vector>

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


typedef std::int_fast64_t i64;
typedef std::uint_fast64_t u64;
typedef std::int_fast32_t i32;
typedef std::uint_fast32_t u32;

namespace tools {
  template <typename T>
  void read(std::vector<T>& vector, const typename std::vector<T>::size_type size) {
    vector.reserve(size);
    std::copy_n(std::istream_iterator<T>(std::cin), size, std::back_inserter(vector));
  }

  template <typename T, std::size_t N>
  void read(std::array<T, N>& array) {
    std::copy_n(std::istream_iterator<T>(std::cin), N, array.begin());
  }
}

#endif

// lib/prim.hpp
#ifndef PRIM_HPP
#define PRIM_HPP


/*
usage:

tools::prim<std::int_fast64_t> prim(node_count);
prim.add_edge(from_node, to_node, cost);
const tools::prim<std::int_fast64_t>::result result = prim.calc();
result.total_distance;
*/

namespace tools {
  template <typename T>
  class prim {
  public:
    class edge {
    private:
      std::size_t m_from;
      std::size_t m_to;
      T m_distance;

    public:
      edge(const std::size_t from, const std::size_t to, const T distance) :
        m_from(from),
        m_to(to),
        m_distance(distance) {
      }

      std::size_t from() const {
        return this->m_from;
      }
      std::size_t to() const {
        return this->m_to;
      }
      T distance() const {
        return this->m_distance;
      }
    };

    class result {
    public:
      T total_distance;
      std::vector<edge> edges;
      result() :
        total_distance(0), edges() {
      }
    };

    std::vector<std::vector<edge>> edges;

    prim(const std::size_t node_count) :
      edges(node_count) {
    }

    std::size_t node_count() const {
      return this->edges.size();
    }

    void add_edge(const std::size_t v1, const std::size_t v2, const T distance) {
      this->edges[v1].emplace_back(v1, v2, distance);
      this->edges[v2].emplace_back(v2, v1, distance);
    }

    result calc() const {
      result result;
      std::vector<bool> visited(this->node_count(), false);
      std::priority_queue<edge, std::vector<edge>, std::function<bool(edge, edge)>> tasks([](const edge& x, const edge& y) {
          return x.distance() > y.distance();
      });
      tasks.emplace(std::numeric_limits<std::size_t>::max(), 0, 0);
      bool is_first_task = true;

      while (!tasks.empty()) {
        const edge task = tasks.top();
        tasks.pop();
        if (visited[task.to()]) continue;
        if (is_first_task) {
          is_first_task = false;
        } else {
          result.total_distance += task.distance();
          result.edges.push_back(task);
        }
        visited[task.to()] = true;
        for (const edge& edge : this->edges[task.to()]) {
          if (!visited[edge.to()]) {
            tasks.push(edge);
          }
        }
      }

      return result;
    }
  };
}

#endif

// main.cpp

class city {
public:
  i64 id, x, y;
};

namespace std {
  std::istream& operator>>(std::istream& is, city& self) {
    static i64 next_id = 0;
    self.id = next_id++;
    return is >> self.x >> self.y;
  }

  template <>
  struct hash<std::pair<i64, i64>> {
    std::size_t operator()(const std::pair<i64, i64>& self) const {
      static std::hash<i64> hash;
      std::size_t result = 17;
      result = 31 * result + hash(self.first);
      result = 31 * result + hash(self.second);
      return result;
    }
  };
}

int main() {
  i64 N;
  std::cin >> N;
  std::vector<city> cities;
  tools::read(cities, N);

  tools::prim<i64> prim(N);
  std::unordered_map<std::pair<i64, i64>, i64> map;
  std::sort(cities.begin(), cities.end(), [](const city& city1, const city& city2) { return city1.x < city2.x; });
  for (auto it1 = cities.begin(), it2 = ++cities.begin(); it2 != cities.end(); ++it1, ++it2) {
    std::pair<i64, i64> pair = std::make_pair(std::min(it1->id, it2->id), std::max(it1->id, it2->id));
    if (map.find(pair) != map.end()) {
      map[pair] = std::min(map[pair], it2->x - it1->x);
    } else {
      map.insert(std::make_pair(pair, it2->x - it1->x));
    }
  }
  std::sort(cities.begin(), cities.end(), [](const city& city1, const city& city2) { return city1.y < city2.y; });
  for (auto it1 = cities.begin(), it2 = ++cities.begin(); it2 != cities.end(); ++it1, ++it2) {
    std::pair<i64, i64> pair = std::make_pair(std::min(it1->id, it2->id), std::max(it1->id, it2->id));
    if (map.find(pair) != map.end()) {
      map[pair] = std::min(map[pair], it2->y - it1->y);
    } else {
      map.insert(std::make_pair(pair, it2->y - it1->y));
    }
  }
  for (const auto& kv : map) {
    prim.add_edge(kv.first.first, kv.first.second, kv.second);
  }
  std::cout << prim.calc().total_distance << std::endl;
  return 0;
}

Measured memory_bytes: 35908