#include using namespace std; using ll = long long; using ld = long double; #define rep(i, a, b) fo(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef pair pii; typedef vector vi; const int N = 2e6 + 10; const int A = 1e6 + 1; void ProGamerMove() { int n; cin >> n; vector> xs(N); for (int x, y, i = 0; i < n; ++i) { cin >> x >> y; xs[x + A].push_back(y); } bool have = false; ll a = 0, b = 0, c = 0, d = 0; ll res = 0, lx = 0; for (int x = 0; x < N; ++x) { if (xs[x].size()) { sort(xs[x].begin(), xs[x].end()); ll diff = abs(xs[x][0] - xs[x].back()); if (!have) { have = true; a = diff; b = diff; c = xs[x][0]; d = xs[x].back(); lx = x; continue; } ll na = min(a + abs(c - xs[x].back()) + diff, b + abs(d - xs[x].back()) + diff); ll nb = min(a + abs(c - xs[x][0]) + diff, b + abs(d - xs[x][0]) + diff); c = xs[x][0]; d = xs[x].back(); a = na; b = nb; res += x - lx; lx = x; } } cout << min(a, b) + res << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int tc = 1; // cin >> tc; while(tc--) ProGamerMove(); }