#include <bits/stdc++.h>
template <class T>
void minimize(T &a, T b) {
  if (a > b) a = b;
}
const int INF = 0x3f3f3f3f;
const int N = 2000;
const int M = 10000;
const int S = 715;
const int A = 9;
int n, from[N], to[N], num[A + 1][A + 1][A + 1][A + 1], four_ind[M], size,
    fl[S][4], d[N + 1][S][A];
int main() {
  for (int i = 0; i < 10; ++i)
    for (int j = 0; j < 10; ++j)
      for (int k = 0; k < 10; ++k)
        for (int l = 0; l < 10; ++l) {
          num[i][j][k][l] = i + j * 10 + k * 100 + l * 1000;
          if (i <= j && j <= k && k <= l) {
            fl[size][0] = i;
            fl[size][1] = j;
            fl[size][2] = k;
            fl[size][3] = l;
            four_ind[num[i][j][k][l]] = size++;
          } else {
            int st[4] = {i, j, k, l};
            std::sort(st, st + 4);
            four_ind[num[i][j][k][l]] =
                four_ind[num[st[0]][st[1]][st[2]][st[3]]];
          }
        }
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", from + i, to + i);
    --from[i];
    --to[i];
  }
  memset(**d, 0x3f, (N + 1) * S * A << 2);
  d[0][size - 1][0] = 0;
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j < size; ++j) {
      for (int k = 1; k < A; ++k) minimize(d[i][j][k], d[i][j][k - 1] + 1);
      for (int k = A - 1; k >= 0; --k) {
        if (k != A - 1) minimize(d[i][j][k], d[i][j][k + 1] + 1);
        int cur[4];
        memcpy(cur, fl[j], 16);
        int out = 0;
        for (int l = 0; l < 4; ++l)
          if (cur[l] == k) {
            cur[l] = 9;
            ++out;
          }
        std::sort(cur, cur + 4);
        int in;
        for (in = 0;
             in < 4 && cur[3 - in] == 9 && i + in < n && from[i + in] == k;
             ++in)
          cur[3 - in] = to[i + in];
        minimize(d[i + in][four_ind[num[cur[0]][cur[1]][cur[2]][cur[3]]]][k],
                 d[i][j][k] + out + in);
      }
    }
  int ans = INF;
  for (int i = 0; i < A; ++i) minimize(ans, d[n][size - 1][i]);
  printf("%d\n", ans);
  return 0;
}
