#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int n, a[N], b[N];
map<string, int> dp[N][10];
int dfs(int idx, int cur, const string& s) {
  if (idx > n && s == "0000") {
    return 0;
  }
  int& res = dp[idx][cur][s];
  if (res) return res;
  res = 0x3f3f3f3f;
  for (char c = '1'; c <= '9'; c++) {
    string t = s;
    int x = 0;
    for (int i = 0; i < 4; i++) {
      if (t[i] == c) t[i] = '0', x++;
    }
    int now = idx;
    for (int i = 0; i < 4; i++) {
      if (t[i] == '0' && now <= n && a[now] + '0' == c) {
        t[i] = b[now++] + '0';
        x++;
      }
    }
    sort(t.begin(), t.end());
    if (idx == now && t == s) continue;
    res = min(res, abs(cur - (c - '0')) + x + dfs(now, c - '0', t));
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", a + i, b + i);
  }
  printf("%d\n", dfs(1, 1, "0000"));
}
