#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int mx = INT_MAX;
const long double pi = 4 * atan((long double)1);
int n;
bool tired[2005][10][10][10][10][10];
int from[2005], to[2005];
struct state {
  int Time;
  int pp;
  int x, y, z, t;
  int cur;
};
void thunder_charge(int &x, int &y, int &z, int &t) {
  if (x > y) swap(x, y);
  if (y > z) swap(y, z);
  if (z > t) swap(z, t);
  if (x > y) swap(x, y);
  if (y > z) swap(y, z);
  if (x > y) swap(x, y);
}
void thunder_bolt(queue<state> &q, int Time, int pp, int x, int y, int z, int t,
                  int cur) {
  if (pp == n and x == 0 and y == 0 and z == 0 and t == 0) {
    throw Time;
  }
  thunder_charge(x, y, z, t);
  if (tired[pp][x][y][z][t][cur]) return;
  tired[pp][x][y][z][t][cur] = true;
  state poke_ball;
  poke_ball.Time = Time;
  poke_ball.pp = pp;
  poke_ball.x = x;
  poke_ball.y = y;
  poke_ball.z = z;
  poke_ball.t = t;
  poke_ball.cur = cur;
  q.push(poke_ball);
}
void bfs() {
  queue<state> q;
  thunder_bolt(q, 0, 0, 0, 0, 0, 0, 1);
  while (!q.empty()) {
    auto poke_ball = q.front();
    q.pop();
    tired[poke_ball.pp][poke_ball.x][poke_ball.y][poke_ball.z][poke_ball.t]
         [poke_ball.cur] = 1;
    if (poke_ball.cur < 9) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp, poke_ball.x,
                   poke_ball.y, poke_ball.z, poke_ball.t, poke_ball.cur + 1);
      ;
    }
    if (poke_ball.cur > 1) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp, poke_ball.x,
                   poke_ball.y, poke_ball.z, poke_ball.t, poke_ball.cur - 1);
      ;
    }
    if (poke_ball.x == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp, 0, poke_ball.y,
                   poke_ball.z, poke_ball.t, poke_ball.cur);
      ;
    }
    if (poke_ball.y == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp, poke_ball.x, 0,
                   poke_ball.z, poke_ball.t, poke_ball.cur);
      ;
    }
    if (poke_ball.z == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp, poke_ball.x,
                   poke_ball.y, 0, poke_ball.t, poke_ball.cur);
      ;
    }
    if (poke_ball.t == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp, poke_ball.x,
                   poke_ball.y, poke_ball.z, 0, poke_ball.cur);
      ;
    }
    if (poke_ball.x == 0 and poke_ball.pp < n and
        from[poke_ball.pp + 1] == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp + 1,
                   to[poke_ball.pp + 1], poke_ball.y, poke_ball.z, poke_ball.t,
                   poke_ball.cur);
      ;
    }
    if (poke_ball.y == 0 and poke_ball.pp < n and
        from[poke_ball.pp + 1] == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp + 1, poke_ball.x,
                   to[poke_ball.pp + 1], poke_ball.z, poke_ball.t,
                   poke_ball.cur);
      ;
    }
    if (poke_ball.z == 0 and poke_ball.pp < n and
        from[poke_ball.pp + 1] == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp + 1, poke_ball.x,
                   poke_ball.y, to[poke_ball.pp + 1], poke_ball.t,
                   poke_ball.cur);
      ;
    }
    if (poke_ball.t == 0 and poke_ball.pp < n and
        from[poke_ball.pp + 1] == poke_ball.cur) {
      thunder_bolt(q, poke_ball.Time + 1, poke_ball.pp + 1, poke_ball.x,
                   poke_ball.y, poke_ball.z, to[poke_ball.pp + 1],
                   poke_ball.cur);
      ;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> from[i] >> to[i];
  try {
    bfs();
  } catch (int ans) {
    cout << ans;
  }
  return 0;
}
