#include <bits/stdc++.h>
using namespace std;
const int fpow[] = {1, 10, 100, 1000};
int n, goal[10], a[2020], b[2020], f[3][10010][10];
int main() {
  memset(f, 0x3f, sizeof(f));
  f[0][0][1] = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i], &b[i]);
  for (int i = 0, pos = 0; i <= n; ++i, pos ^= 1) {
    memset(f[pos ^ 1], 0x3f, sizeof(f[pos ^ 1]));
    for (int s = 9999; s >= 0; --s)
      for (int k = 1; k <= 9; ++k)
        if (f[pos][s][k] != 0x3f3f3f3f) {
          int state = s, num = 0, ns, p = 0;
          for (int x = 1; x <= 4; ++x) {
            goal[x] = state % 10;
            state /= 10;
            if (goal[x])
              ++num;
            else
              p = x;
          }
          if (num < 4) {
            ns = s + fpow[p - 1] * b[i + 1];
            f[pos ^ 1][ns][a[i + 1]] = min(
                f[pos ^ 1][ns][a[i + 1]], f[pos][s][k] + abs(k - a[i + 1]) + 1);
          }
          for (int x = 1; x <= 4; ++x)
            if (goal[x]) {
              ns = s - goal[x] * fpow[x - 1];
              f[pos][ns][goal[x]] =
                  min(f[pos][ns][goal[x]], f[pos][s][k] + abs(goal[x] - k) + 1);
            }
        }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= 9; ++i) ans = min(ans, f[n & 1][0][i]);
  printf("%d\n", ans);
  return 0;
}
