#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize(2)
#pragma G++ optimize(2)
using namespace std;
constexpr long long N = 2e3 + 9;
constexpr long long M = 998244353;
constexpr long long INF = 0x3f3f3f3f3f3f3f3f;
constexpr long long INF2 = 0xcfcfcfcfcfcfcfcf;
long long n;
long long a[N], b[N];
long long dis[N][10][800];
long long tmp[4];
long long id[10][10][10][10];
long long tot = 0;
void init() {
  for (long long i = 0; i <= 9; i++)
    for (long long j = i; j <= 9; j++)
      for (long long k = j; k <= 9; k++)
        for (long long l = k; l <= 9; l++) id[i][j][k][l] = tot++;
  memset(dis, 0x3f, sizeof(dis));
}
long long get(long long a, long long b, long long c, long long d) {
  tmp[0] = a, tmp[1] = b, tmp[2] = c, tmp[3] = d;
  sort(tmp, tmp + 4);
  return id[tmp[0]][tmp[1]][tmp[2]][tmp[3]];
}
struct State {
  long long i;
  long long l;
  long long t[4];
  long long& G() const {
    long long id = get(t[0], t[1], t[2], t[3]);
    return dis[i][l][id];
  }
};
signed main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i] >> b[i];
  init();
  queue<State> q;
  q.push({0, 1, {0, 0, 0, 0}});
  dis[0][1][get(0, 0, 0, 0)] = 0;
  long long ans = INF;
  while (!q.empty()) {
    State s = q.front();
    q.pop();
    long long raw = s.G();
    if (s.i == n && s.t[0] + s.t[1] + s.t[2] + s.t[3] == 0) ans = min(ans, raw);
    for (long long d : {-1, 1}) {
      if (s.l + d <= 9 && s.l + d >= 1) {
        State s2 = s;
        s2.l += d;
        if (raw + 1 < s2.G()) s2.G() = raw + 1, q.push(s2);
      }
    }
    long long empty = -1;
    for (long long i = 0; i < 4; i++) {
      if (s.t[i] == s.l) {
        State s2 = s;
        s2.t[i] = 0;
        if (raw + 1 < s2.G()) s2.G() = raw + 1, q.push(s2);
      }
      if (s.t[i] == 0) empty = i;
    }
    for (long long i = 0; i < 4; i++) {
      if (s.i + 1 <= n && empty >= 0 && a[s.i + 1] == s.l) {
        State s2 = s;
        s2.i++;
        s2.t[empty] = b[s2.i];
        if (raw + 1 < s2.G()) s2.G() = raw + 1, q.push(s2);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
