#include <bits/stdc++.h>
using namespace std;
const int N = 2002;
int n, from[N], to[N];
using Arr = array<int, 7>;
bool hsh[N][10][10][10][10][10];
queue<Arr> Q;
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n - 1; ++i) {
    scanf("%d %d", from + i, to + i);
  }
  Arr v = {0, 0, 0, 0, 0, 1, 0};
  Q.push(v);
  int ans = 1e9;
  while (!Q.empty()) {
    v = Q.front();
    Q.pop();
    if (hsh[v[0]][v[1]][v[2]][v[3]][v[4]][v[5]]) continue;
    hsh[v[0]][v[1]][v[2]][v[3]][v[4]][v[5]] = 1;
    if (v[0] == n && v[1] == v[2] && v[2] == v[3] && v[3] == v[4] &&
        v[4] == 0) {
      ans = min(ans, v[6]);
      break;
    }
    if (v[5] < 9) {
      Arr vv = v;
      ++vv[5];
      vv[6] = vv[6] + 1;
      if (!hsh[vv[0]][vv[1]][vv[2]][vv[3]][vv[4]][vv[5]]) {
        Q.push(vv);
      }
    }
    if (v[5] > 1) {
      Arr vv = v;
      --vv[5];
      ++vv[6];
      if (!hsh[vv[0]][vv[1]][vv[2]][vv[3]][vv[4]][vv[5]]) {
        Q.push(vv);
      }
    }
    for (int i = 1; i <= 4; ++i)
      if (v[i] == v[5]) {
        Arr vv = v;
        vv[i] = 0;
        ++vv[6];
        sort(vv.begin() + 1, vv.begin() + 5);
        if (!hsh[vv[0]][vv[1]][vv[2]][vv[3]][vv[4]][vv[5]]) {
          Q.push(vv);
        }
        break;
      }
    if (v[1] == 0 && v[0] < n && v[5] == from[v[0]]) {
      Arr vv = v;
      vv[0] = v[0] + 1;
      vv[1] = to[v[0]];
      ++vv[6];
      sort(vv.begin() + 1, vv.begin() + 5);
      if (!hsh[vv[0]][vv[1]][vv[2]][vv[3]][vv[4]][vv[5]]) {
        Q.push(vv);
      }
    }
  }
  printf("%d\n", ans);
}
