#include <bits/stdc++.h>
using namespace std;
int n, a[2005], b[2005], dp[2][11][11][11][11][11];
void calc(int &a, int b) {
  int tmp = min(a, b);
  a = tmp;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
  memset(dp, 0x3f, sizeof dp);
  int cur = 0;
  dp[cur][1][0][0][0][0] = 2 * n;
  for (int i = 0; i <= n; i++) {
    for (int x = 9; x >= 0; x--)
      for (int y = 9; y >= 0; y--)
        for (int z = 9; z >= 0; z--)
          for (int w = 9; w >= 0; w--)
            for (int f = 1; f <= 9; f++) {
              int now = dp[cur][f][x][y][z][w];
              if (now == 0x3f3f3f3f) continue;
              if (x == 0 && i < n)
                calc(dp[cur ^ 1][a[i + 1]][b[i + 1]][y][z][w],
                     now + abs(f - a[i + 1]));
              else if (x)
                calc(dp[cur][x][0][y][z][w], now + abs(f - x));
              if (y == 0 && i < n)
                calc(dp[cur ^ 1][a[i + 1]][x][b[i + 1]][z][w],
                     now + abs(f - a[i + 1]));
              else if (y)
                calc(dp[cur][y][x][0][z][w], now + abs(f - y));
              if (z == 0 && i < n)
                calc(dp[cur ^ 1][a[i + 1]][x][y][b[i + 1]][w],
                     now + abs(f - a[i + 1]));
              else if (z)
                calc(dp[cur][z][x][y][0][w], now + abs(f - z));
              if (w == 0 && i < n)
                calc(dp[cur ^ 1][a[i + 1]][x][y][z][b[i + 1]],
                     now + abs(f - a[i + 1]));
              else if (w)
                calc(dp[cur][w][x][y][z][0], now + abs(f - w));
            }
    if (i < n) {
      memset(dp[cur], 0x3f, sizeof dp[cur]);
      cur ^= 1;
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= 9; i++) ans = min(ans, dp[cur][i][0][0][0][0]);
  cout << ans << '\n';
}
