#include <bits/stdc++.h>
using namespace std;
map<string, int> f[2005][10];
char A[2005], B[2005];
int n;
int dfs(int nn, int loc, const string &S) {
  if (nn > n && S == "0000") return 0;
  int &res = f[nn][loc][S];
  if (res) return res;
  res = 2e9;
  for (char c = '1'; c <= '9'; ++c) {
    int t = 0, nown = nn;
    string s = S;
    for (int i = 0; i < 4; ++i)
      if (s[i] == c) ++t, s[i] = '0';
    for (int i = 0; i < 4; ++i)
      if (s[i] == '0' && A[nown] == c) s[i] = B[nown++], ++t;
    sort(s.begin(), s.end());
    if (nown == nn && s == S) continue;
    res = min(res, t + abs(c - '0' - loc) + dfs(nown, c - '0', s));
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%s%s", A + i, B + i);
  return !printf("%d\n", dfs(1, 1, "0000"));
}
