#include using namespace std; void reverse(vector> & volcanoes, int from, int to) { vector tmp; while(to >= from) { tmp.push_back(volcanoes[to].second); to--; } for(auto j : tmp) { volcanoes[from].second = j; from++; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, x, y, ret; vector> volcanoes; ret = 0; cin >> n; if(n == 1) { cout << "0\n"; return 0; } while(n--) { cin >> x >> y; volcanoes.push_back(make_pair(x, y)); } sort(volcanoes.begin(), volcanoes.end()); for(size_t i = 0; i < volcanoes.size() - 2; i++) { if(volcanoes[i + 1].first == volcanoes[i + 2].first) { size_t j = i + 1; while(j < volcanoes.size() && volcanoes[i + 1].first == volcanoes[j].first) j++; j--; if(abs(volcanoes[i].second - volcanoes[j].second) < abs(volcanoes[i].second - volcanoes[i + 1].second)) reverse(volcanoes, i + 1, j); i = j; } } x = volcanoes[0].first; y = volcanoes[0].second; for(auto i : volcanoes) { ret += abs(i.first - x); ret += abs(i.second - y); x = i.first; y = i.second; } cout << ret << '\n'; return 0; }