#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000;
int dp[MAXN + 1][10][2100], in[MAXN + 5], out[MAXN + 5], sid, N;
map<int, int> trans, rev;
int set_id(int code) {
  auto it = trans.find(code);
  if (it == trans.end()) {
    trans.emplace(code, sid);
    rev.emplace(sid, code);
    sid++;
    return sid - 1;
  } else
    return it->second;
}
int encode(int arr[4]) {
  int code = 0;
  sort(arr, arr + 4);
  for (int i = 0; i < 4; i++) {
    code *= 10;
    code += arr[i];
  }
  return set_id(code);
}
void decode(int arr[4], int code) {
  code = rev[code];
  for (int i = 0; i < 4; i++) {
    arr[i] = code % 10;
    code /= 10;
  }
}
int dfs(int p, int f, int code);
int test(const int pass[4], int p, int f) {
  int cnt = 0, tmp[4], add = 0;
  for (int i = 0; i < 4; i++) {
    if (pass[i] == f)
      tmp[i] = 0, add++;
    else
      tmp[i] = pass[i];
    if (tmp[i]) cnt++;
  }
  sort(tmp, tmp + 4, greater<int>());
  while (p < N && cnt < 4 && in[p] == f) {
    tmp[cnt] = out[p];
    p++;
    cnt++;
    add++;
  }
  return dfs(p, f, encode(tmp)) + add;
}
int dfs(int p, int f, int code) {
  int pass[4], res = (INT_MAX / 2), cnt = 0;
  bool vis[10] = {0};
  decode(pass, code);
  for (int i = 0; i < 4; i++) cnt += (pass[i] != 0);
  if (cnt == 0 && p == N) return 0;
  if (dp[p][f][code] != 0) return dp[p][f][code];
  if (p < N && cnt < 4) {
    res = min(res, test(pass, p, in[p]) + abs(f - in[p]));
    vis[in[p]] = true;
  }
  for (int i = 0; i < 4; i++) {
    if (pass[i] != 0 && !vis[pass[i]]) {
      res = min(res, test(pass, p, pass[i]) + abs(f - pass[i]));
      vis[pass[i]] = true;
    }
  }
  return dp[p][f][code] = res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> in[i] >> out[i];
  }
  int tmp[] = {0, 0, 0, 0};
  cout << dfs(0, 1, encode(tmp)) << '\n';
}
