#include <bits/stdc++.h>
using namespace std;
int const N = 2000, inf = 1e9;
pair<int, int> p[N];
int dp[N + 1][10][10][10][18], tmp[4];
inline void up(int &x, int y) { x = min(x, y); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i) {
    scanf("%d%d", &p[i].first, &p[i].second);
    --p[i].first;
    --p[i].second;
  }
  for (int i = 0; i < (int)(n + 1); ++i)
    for (int b = 0; b < (int)(10); ++b)
      for (int c = 0; c < (int)(10); ++c)
        for (int d = 0; d < (int)(10); ++d)
          for (int a = 0; a < (int)(18); ++a) dp[i][b][c][d][a] = inf;
  dp[0][9][9][9][9] = 0;
  for (int i = 0; i < (int)(n + 1); ++i)
    for (int b = 0; b < (int)(10); ++b)
      for (int c = 0; c < (int)(10); ++c)
        for (int d = 0; d < (int)(10); ++d)
          for (int a = 0; a < (int)(18); ++a) {
            int &r = dp[i][b][c][d][a];
            if (r >= inf) continue;
            int fr;
            int cf = a >= 9 ? a - 9 : p[i - 1].first, nb, nc, nd, na;
            for (int o = 0; o < (int)(9); ++o) {
              na = a, nb = b, nc = c, nd = d;
              fr = (a >= 9) + (b == 9) + (c == 9) + (d == 9);
              int cst = abs(cf - o), ni = i;
              if (o == a) ++fr, ++cst, na = 9 + o;
              if (o == b) ++fr, ++cst, nb = 9;
              if (o == c) ++fr, ++cst, nc = 9;
              if (o == d) ++fr, ++cst, nd = 9;
              while (ni < n && fr && p[ni].first == o) {
                --fr;
                ++cst;
                if (nb == 9)
                  nb = p[ni].second;
                else if (nc == 9)
                  nc = p[ni].second;
                else if (nd == 9)
                  nd = p[ni].second;
                else
                  na = p[ni].second;
                ++ni;
              }
              tmp[0] = nb;
              tmp[1] = nc;
              tmp[2] = nd;
              tmp[3] = na;
              sort(tmp, tmp + 4);
              nb = tmp[0];
              nc = tmp[1];
              nd = tmp[2];
              na = tmp[3];
              if (fr) na = o + 9;
              up(dp[ni][nb][nc][nd][na], r + cst);
            }
          }
  int an = inf;
  for (int i = 9; i < (int)(18); ++i) an = min(an, dp[n][9][9][9][i]);
  printf("%d\n", an);
}
