#include <bits/stdc++.h>
struct edge {
  long long int to, cost;
};
const long long int inf = 1123456789012345;
long long int N;
std::vector<std::pair<long long int, long long int>> xp, yp;
std::vector<std::vector<edge>> G;
std::vector<long long int> dist;
std::map<long long int, long long int> tmp1, tmp2;
void dijkstra(long long int s) {
  std::priority_queue<std::pair<long long int, long long int>,
                      std::vector<std::pair<long long int, long long int>>,
                      std::greater<std::pair<long long int, long long int>>>
      que;
  std::fill(dist.begin(), dist.end(), inf);
  dist[s] = 0;
  que.push(std::pair<long long int, long long int>(0, s));
  while (!que.empty()) {
    std::pair<long long int, long long int> p = que.top();
    que.pop();
    long long int v = p.second;
    if (dist[v] < p.first) {
      continue;
    }
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(std::pair<long long int, long long int>(dist[e.to], e.to));
      }
    }
  }
}
int main() {
  std::cin >> N;
  std::vector<long long int> x(N), y(N);
  G.resize(2 * N + 1);
  dist.resize(2 * N + 1);
  for (int i = 0; i < N; ++i) {
    std::cin >> x[i] >> y[i];
    xp.push_back(std::make_pair(x[i], i + 1));
    yp.push_back(std::make_pair(y[i], i + 1 + N));
  }
  std::sort(xp.begin(), xp.end());
  std::sort(yp.begin(), yp.end());
  for (int i = 0; i < N - 1; ++i) {
    G[i + 1].push_back({i + 2, abs(xp[i].first - xp[i + 1].first)});
    G[i + 2].push_back({i + 1, abs(xp[i].first - xp[i + 1].first)});
    G[i + 1 + N].push_back({i + 2 + N, abs(yp[i].first - yp[i + 1].first)});
    G[i + 2 + N].push_back({i + 1 + N, abs(yp[i].first - yp[i + 1].first)});
  }
  for (int i = 0; i < N; ++i) {
    tmp1[xp[i].first] = i + 1;
    tmp2[yp[i].first] = i + 1 + N;
  }
  for (int i = 0; i < N; ++i) {
    G[tmp1[x[i]]].push_back({tmp2[y[i]], 0});
    G[tmp2[y[i]]].push_back({tmp1[x[i]], 0});
  }
  G[0].push_back({tmp1[x[0]], 0});
  G[0].push_back({tmp2[y[0]], 0});
  dijkstra(0);
  std::cout << std::min(dist[N], dist[2 * N]) << std::endl;
  return 0;
}
