#include <bits/stdc++.h>
using namespace std;

const int N = 200010;
const int INF = 0x3f3f3f3f;

int n, L[N], R[N];
long long dp[N][2];

int main() {
	memset(L, 0x3f, sizeof(L));
	memset(R, -0x3f, sizeof(R));

	scanf("%d", &n);
	for (int i = 0, x, c; i < n; ++i) {
		scanf("%d%d", &x, &c);
		L[c] = min(L[c], x);
		R[c] = max(R[c], x);
	}
	L[0] = R[0] = 0;
	memset(dp, 0x3f3f, sizeof(dp));
	dp[0][0] = dp[0][1] = 0;
	for (int i = 1; i <= n; ++i) {
		if (L[i] == INF) {
			dp[i][0] = dp[i - 1][0];
			dp[i][1] = dp[i - 1][1];
			L[i] = L[i - 1];
			R[i] = R[i - 1];
			continue;
		}
		dp[i][0] = min(dp[i - 1][0] + abs(R[i] - L[i - 1]) + R[i] - L[i],
					   dp[i - 1][1] + abs(R[i] - R[i - 1]) + R[i] - L[i]);
		dp[i][1] = min(dp[i - 1][0] + abs(L[i] - L[i - 1]) + R[i] - L[i],
					   dp[i - 1][1] + abs(L[i] - R[i - 1]) + R[i] - L[i]);
	}
	printf("%lld\n", min(dp[n][0] + abs(L[n]), dp[n][1] + abs(R[n])));
}
#include <bits/stdc++.h>
using namespace std;

typedef complex<double> P;
#define X real()
#define Y imag()

// 内積 |a||b|cos(theta)
double dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }

// 外積 |a||b|sin(theta)
double cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }

int main() {
    int N;
    cin >> N;
    vector<P> A(N), B(N);
    for (auto &x : A) {
        int a, b;
        cin >> a >> b;
        x = P(a, b);
    }
    for (auto &x : B) {
        int a, b;
        cin >> a >> b;
        x = P(a, b);
    }
    map<tuple<int, int, int>, int> s, t;
    auto add = [](auto &s, auto a1, auto a2, auto a3) {
        // 2点間のベクトル
        auto v1 = a2 - a1;
        auto v2 = a3 - a1;
        // 外積
        int cr = cross(v1, v2);
        // 2点間の距離(整数にするため２乗)
        int r1 = abs(v1) * abs(v1);
        int r2 = abs(v2) * abs(v2);
        s[{cr, r1, r2}]++;
    };
    for (auto a : A)
        for (auto b : A)
            for (auto c : A)
                add(s, a, b, c);
    for (auto a : B)
        for (auto b : B)
            for (auto c : B)
                add(t, a, b, c);
    puts(s == t ? "Yes" : "No");
}