#include <bits/stdc++.h>
using namespace std;
int n;
int dp[2][10][10000];
int s[2001], e[2001];
bool isFull(int m) { return m % 10 > 0; }
bool isEmpty(int m) { return m == 0; }
int getIn(int m, int f) {
  int i;
  vector<int> a(4);
  for (i = 0; i < 4; i++) {
    a[i] = m % 10;
    m /= 10;
  }
  for (i = 0; i < 4; i++) {
    if (a[i] == 0) {
      a[i] = f;
      break;
    }
  }
  sort(a.begin(), a.end());
  int ret = 0;
  for (i = 3; i >= 0; i--) {
    ret *= 10;
    ret += a[i];
  }
  return ret;
}
set<int> getDests(int m) {
  set<int> ret;
  while (m > 0) {
    if (m % 10 != 0) ret.insert(m % 10);
    m /= 10;
  }
  return ret;
}
pair<int, int> getOut(int m, int f) {
  int i, o = 0;
  vector<int> a(4);
  for (i = 0; i < 4; i++) {
    a[i] = m % 10;
    m /= 10;
    if (a[i] == f) {
      a[i] = 0;
      o++;
    }
  }
  sort(a.begin(), a.end());
  int ret = 0;
  for (i = 3; i >= 0; i--) {
    ret *= 10;
    ret += a[i];
  }
  return make_pair(ret, o);
}
int main() {
  int i, f, m, r;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> s[i] >> e[i];
  }
  for (i = 0; i <= 1; i++) {
    for (f = 0; f < 10; f++) {
      for (m = 0; m < 10000; m++) {
        dp[i][f][m] = 999999999;
      }
    }
  }
  dp[0][1][0] = 0;
  dp[1][s[1]][e[1] * 1000] = s[1];
  int tm, to, ii;
  pair<int, int> p;
  for (i = 1; i <= n; i++) {
    ii = i % 2;
    for (f = 0; f < 10; f++) {
      for (m = 0; m < 10000; m++) {
        dp[1 - ii][f][m] = 999999999;
      }
    }
    for (m = 9999; m >= 0; m--) {
      for (f = 1; f <= 9; f++) {
        if (dp[ii][f][m] == 999999999) continue;
        if (!isEmpty(m)) {
          set<int> ts = getDests(m);
          for (auto d : ts) {
            p = getOut(m, d);
            tm = p.first;
            to = p.second;
            dp[ii][d][tm] = min(dp[ii][d][tm], dp[ii][f][m] + abs(f - d) + to);
          }
        }
        if (!isFull(m) && i < n) {
          tm = getIn(m, e[i + 1]);
          dp[1 - ii][s[i + 1]][tm] = min(dp[1 - ii][s[i + 1]][tm],
                                         dp[ii][f][m] + abs(f - s[i + 1]) + 1);
        }
      }
    }
  }
  int ans = 9999999999;
  for (f = 1; f < 10; f++) {
    ans = min(ans, dp[n % 2][f][0]);
  }
  cout << ans;
  return 0;
}
