#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2005;
const int inf = 0x3f3f3f3f;
int a[maxn], b[maxn], n;
int tot;
int x[10];
int c[1005], N;
int dp[2005][730];
void dfs(int k) {
  if (k == 10) {
    int state = 0;
    for (int i = 1; i <= 9; i++) state |= (x[i] << (3 * i - 3));
    c[N++] = state;
    return;
  }
  for (int i = 0; i + tot <= 4; i++) {
    x[k] = i;
    tot += i;
    dfs(k + 1);
    tot -= i;
  }
}
int Hash(int x) { return lower_bound(c, c + N, x) - c; }
int cal(int state, int i) { return (state >> (3 * i - 3)) & 7; }
int h[5], siz, Count;
bool vis[5];
void spilt(int state) {
  siz = Count = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= 9; i++) {
    int tt = state & 7;
    Count += tt;
    if (tt) h[++siz] = i;
    state >>= 3;
  }
  while (Count > 4)
    ;
}
int goal;
void dfs2(int k, int state, int prepos, int cost) {
  if (k == siz + 1) {
    if (goal == n + 1) {
      if (state == 0) dp[goal][0] = min(dp[goal][0], cost);
    } else if (Count < 4) {
      int num = cal(state, b[goal]);
      state ^= (num << (3 * b[goal] - 3)) ^ ((num + 1) << (3 * b[goal] - 3));
      int j = Hash(state);
      dp[goal][j] = min(dp[goal][j], cost + abs(a[goal] - prepos) + 1);
    }
    return;
  }
  dfs2(k + 1, state, prepos, cost);
  for (int i = 1; i <= siz; i++) {
    if (vis[i]) continue;
    int people = cal(state, h[i]);
    Count -= people;
    vis[i] = true;
    dfs2(k + 1, state ^ (people << (3 * h[i] - 3)), h[i],
         cost + abs(prepos - h[i]) + people);
    vis[i] = false;
    Count += people;
  }
}
int main() {
  ios::sync_with_stdio(false);
  dfs(1);
  sort(c, c + N);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
  a[0] = 1;
  memset(dp, inf, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < N; j++) {
      if (dp[i][j] == inf) continue;
      spilt(c[j]);
      goal = i + 1;
      dfs2(1, c[j], a[i], dp[i][j]);
    }
  }
  cout << dp[n + 1][0] << endl;
  return 0;
}
