#include <bits/stdc++.h>
using namespace std;
const int N = 2000 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);
int dp[2][10][10][10][10][10];
int n, a[N], b[N];
int cur = 0;
inline bool chkmin(int& a, int b) { return a > b ? a = b, true : false; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]);
  memset(dp[cur], inf, sizeof(dp[cur]));
  dp[cur][1][0][0][0][0] = 2 * n;
  for (int i = 0; i <= n; i++) {
    cur ^= 1;
    memset(dp[cur], 0x3f, sizeof dp[cur]);
    for (int c1 = 9; c1 >= 0; c1--) {
      for (int c2 = 9; c2 >= 0; c2--) {
        for (int c3 = 9; c3 >= 0; c3--) {
          for (int c4 = 9; c4 >= 0; c4--) {
            for (int f = 1; f <= 9; f++) {
              int& ret = dp[cur ^ 1][f][c1][c2][c3][c4];
              if (ret >= inf) continue;
              if (!c1 && i < n) {
                chkmin(dp[cur][a[i + 1]][b[i + 1]][c2][c3][c4],
                       ret + abs(f - a[i + 1]));
              } else if (c1) {
                chkmin(dp[cur ^ 1][c1][0][c2][c3][c4], ret + abs(f - c1));
              }
              if (!c2 && i < n) {
                chkmin(dp[cur][a[i + 1]][c1][b[i + 1]][c3][c4],
                       ret + abs(f - a[i + 1]));
              } else if (c2) {
                chkmin(dp[cur ^ 1][c2][c1][0][c3][c4], ret + abs(f - c2));
              }
              if (!c3 && i < n) {
                chkmin(dp[cur][a[i + 1]][c1][c2][b[i + 1]][c4],
                       ret + abs(f - a[i + 1]));
              } else if (c3) {
                chkmin(dp[cur ^ 1][c3][c1][c2][0][c4], ret + abs(f - c3));
              }
              if (!c4 && i < n) {
                chkmin(dp[cur][a[i + 1]][c1][c2][c3][b[i + 1]],
                       ret + abs(f - a[i + 1]));
              } else if (c4) {
                chkmin(dp[cur ^ 1][c4][c1][c2][c3][0], ret + abs(f - c4));
              }
            }
          }
        }
      }
    }
  }
  int ans = inf;
  for (int i = 1; i <= 9; i++) ans = min(ans, dp[cur ^ 1][i][0][0][0][0]);
  printf("%d\n", ans);
  return 0;
}
