#include <bits/stdc++.h>
using namespace std;
int inf = pow(10, 9);
int modulo = pow(10, 9) + 7;
long double eps = 1e-15;
ifstream in;
ofstream out;
int mx = pow(10, 4);
int hsh(int mertebe, int bit) { return bit * 10 + mertebe; }
int dehsh_bit(int a) { return a / 10; }
int dehsh_mertebe(int a) { return a % 10; }
void deal() {
  int n;
  cin >> n;
  int dp[mx][10];
  int dp1[mx][10];
  for (long long int j = 0; j < mx; j++)
    for (long long int k = 0; k < 10; k++) dp[j][k] = inf;
  for (int i = 1; i < 10; i++) dp[0][i] = i - 1;
  for (long long int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    vector<int> qw;
    for (long long int i = 0; i < mx; i++)
      for (long long int j = 0; j < 10; j++) dp1[i][j] = inf;
    multimap<int, int> dij;
    for (long long int j = 0; j < mx; j++) {
      for (long long int k = 0; k < 4; k++) {
        int z = pow(10, k);
        if ((j / z) % 10 == 0) {
          int yj = j + b * pow(10, k);
          if (dp[j][a] + 1 < dp1[yj][a]) {
            dp1[yj][a] = dp[j][a] + 1;
            dij.insert(make_pair(dp1[yj][a], hsh(a, yj)));
          }
          break;
        }
      }
    }
    while (dij.size()) {
      int wh = (*dij.begin()).second;
      dij.erase(dij.begin());
      int mertebe = dehsh_mertebe(wh);
      int bit = dehsh_bit(wh);
      int yen = bit;
      int tim = dp1[bit][mertebe];
      for (long long int j = 0; j < 4; j++) {
        int z = pow(10, j);
        int ql = (bit / z) % 10;
        if (ql == mertebe) yen -= mertebe * z, ++tim;
      }
      if (yen != bit) {
        if (tim < dp1[yen][mertebe]) {
          dp1[yen][mertebe] = tim;
          dij.insert(make_pair(dp1[yen][mertebe], hsh(mertebe, yen)));
        }
      } else {
        if (mertebe != 1) {
          if (dp1[yen][mertebe] + 1 < dp1[yen][mertebe - 1]) {
            dp1[yen][mertebe - 1] = dp1[yen][mertebe] + 1;
            dij.insert(
                make_pair(dp1[yen][mertebe - 1], (hsh(mertebe - 1, yen))));
          }
        }
        if (mertebe != 9) {
          if (dp1[yen][mertebe] + 1 < dp1[yen][mertebe + 1]) {
            dp1[yen][mertebe + 1] = dp1[yen][mertebe] + 1;
            dij.insert(make_pair(dp1[yen][mertebe + 1], hsh(mertebe + 1, yen)));
          }
        }
      }
    }
    for (long long int i = 0; i < mx; i++)
      for (long long int j = 0; j < 10; j++) dp[i][j] = dp1[i][j];
  }
  int mn = inf;
  for (long long int i = 0; i < 10; i++) mn = min(mn, dp[0][i]);
  cout << mn;
}
int main() { deal(); }
