#include #include #include struct Coord { int x; int y; }; bool compareCoords(const Coord &lhs, const Coord &rhs) { if (lhs.x < rhs.x) return true; if (lhs.x > rhs.x) return false; return lhs.y < rhs.y; } struct PointDistance { int y = 0; long long d = 0; }; int main() { int N; std::cin >> N; std::vector coords(N); for (int i = 0; i < N; ++i) { std::cin >> coords[i].x >> coords[i].y; } std::sort(coords.begin(), coords.end(), compareCoords); long long distance = coords.back().x - coords.front().x; PointDistance last_low, last_high; int current_x = coords[0].x; bool first_iter = true; for (int i = 0; i < N;) { PointDistance low, high; low.y = high.y = coords[i].y; while (i < N && coords[i].x == current_x) { low.y = std::min(low.y, coords[i].y); high.y = std::max(high.y, coords[i].y); ++i; } distance += (high.y - low.y); if (first_iter) { last_low.y = low.y; last_high.y = high.y; first_iter = false; } low.d = std::min(last_low.d + abs(high.y - last_low.y), last_high.d + abs(high.y - last_high.y)); high.d = std::min(last_low.d + abs(low.y - last_low.y), last_high.d + abs(low.y - last_high.y)); last_low = low; last_high = high; if (i < N) current_x = coords[i].x; } distance += std::min(last_low.d, last_high.d); std::cout << distance << '\n'; return 0; }