#include <bits/stdc++.h>
using namespace std;
const int MX = 2005;
const int INF = 1000000000;
int a[MX], b[MX];
int dp[MX][715][10];
vector<vector<int>> V;
void gen(int i, vector<int> x) {
  if (i == 10) {
    V.push_back(x);
    return;
  }
  while (x.size() <= 4) {
    gen(i + 1, x);
    x.push_back(i);
  }
}
bool comp(vector<int>& x, vector<int>& y) { return x.size() < y.size(); }
int main() {
  gen(1, vector<int>(0));
  sort(V.begin(), V.end(), comp);
  int n;
  ignore = scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    ignore = scanf("%d %d", a + i, b + i);
  }
  for (int i = 0; i < MX; i++) {
    for (int j = 0; j < 715; j++) {
      for (int k = 0; k < 10; k++) {
        dp[i][j][k] = INF;
      }
    }
  }
  dp[0][0][1] = 0;
  for (int c = 0; c <= n; c++) {
    for (int i = 714; i >= 0; i--) {
      for (int cur = 1; cur <= 9; cur++) {
        if (dp[c][i][cur] == INF) continue;
        for (int to = 1; to <= 9; to++) {
          int tm = abs(to - cur);
          vector<int> NW;
          for (int x : V[i]) {
            if (x == to)
              tm++;
            else
              NW.push_back(x);
          }
          int j;
          for (j = c + 1; j <= n && a[j] == to && NW.size() < 4; j++) {
            NW.push_back(b[j]);
            tm++;
          }
          sort(NW.begin(), NW.end());
          int new_c = j - 1;
          int new_i = 0;
          while (V[new_i] != NW) new_i++;
          dp[new_c][new_i][to] = min(dp[new_c][new_i][to], dp[c][i][cur] + tm);
        }
      }
    }
  }
  int ans = INF;
  for (int i = 1; i <= 9; i++) {
    ans = min(ans, dp[n][0][i]);
  }
  printf("%d\n", ans);
}
