#include <bits/stdc++.h>
using namespace std;
const int MN = 2005, MM = 10005, inf = 1000000005, MOD = 1000000007, MF = 10;
const long long INF = 1000000000000000005LL;
int dp[2][MM][MF];
int from[MN], to[MN];
int dod[MM][MF], wyw[MM][MF];
int p10[MF];
queue<pair<short int, char> > Q[MN];
int main() {
  p10[0] = 1;
  for (int j = 1; j <= 9; ++j) p10[j] = 10 * p10[j - 1];
  for (int i = 0; i < MM; ++i)
    for (int j = 1; j <= 9; ++j) {
      dod[i][j] = wyw[i][j] = i;
      for (int p = 0; p < 4; ++p)
        if ((dod[i][j] / p10[p]) % 10 == 0) {
          dod[i][j] += p10[p] * j;
          break;
        }
      for (int p = 0; p < 4; ++p)
        if ((wyw[i][j] / p10[p]) % 10 == j) wyw[i][j] -= p10[p] * j;
    }
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d%d", &from[i], &to[i]);
  for (int i = 0; i < MM; ++i)
    for (int j = 1; j <= 9; ++j) dp[0][i][j] = dp[1][i][j] = inf;
  dp[1][0][1] = 0;
  Q[1].push({0, 1});
  for (int x = 1; x <= n + 1; ++x) {
    int chce = from[x], jade = to[x];
    int p = x & 1;
    while (!Q[p].empty()) {
      auto stan = Q[p].front();
      Q[p].pop();
      if (stan.second == chce && dod[stan.first][jade] > stan.first) {
        if (dp[p ^ 1][dod[stan.first][jade]][chce] > dp[p][stan.first][chce]) {
          Q[p ^ 1].push({dod[stan.first][jade], chce});
          dp[p ^ 1][dod[stan.first][jade]][chce] = dp[p][stan.first][chce];
        }
      }
      if (stan.second > 1 &&
          dp[p][wyw[stan.first][stan.second - 1]][stan.second - 1] >
              dp[p][stan.first][stan.second] + 1) {
        dp[p][wyw[stan.first][stan.second - 1]][stan.second - 1] =
            dp[p][stan.first][stan.second] + 1;
        Q[p].push({wyw[stan.first][stan.second - 1], stan.second - 1});
      }
      if (stan.second < 9 &&
          dp[p][wyw[stan.first][stan.second + 1]][stan.second + 1] >
              dp[p][stan.first][stan.second] + 1) {
        dp[p][wyw[stan.first][stan.second + 1]][stan.second + 1] =
            dp[p][stan.first][stan.second] + 1;
        Q[p].push({wyw[stan.first][stan.second + 1], stan.second + 1});
      }
    }
    if (x == n + 1) break;
    for (int i = 0; i < MM; ++i)
      for (int j = 1; j <= 9; ++j) dp[p][i][j] = inf;
  }
  int res = inf;
  ++n;
  for (int j = 1; j <= 9; ++j) res = min(res, dp[n & 1][0][j]);
  --n;
  printf("%d", res + 2 * n);
  return 0;
}
