#include <bits/stdc++.h>
using namespace std;
inline long long GCD(long long a, long long b) {
  long long n;
  if (a < b) swap(a, b);
  while (b != 0) {
    n = a % b;
    a = b;
    b = n;
  }
  return a;
}
inline long long LCM(long long a, long long b) {
  if (a == 0 || b == 0) return GCD(a, b);
  return a / GCD(a, b) * b;
}
inline long long CEIL(long long n, long long d) { return n / d + n % d != 0; }
inline long long ROUND(long long n, long long d) {
  return n / d + (long long)((n % d) * 2 >= d);
}
inline long long POW(long long a, long long n) {
  if (n < 0) return 0;
  long long ret = 1;
  while (n) {
    if (n % 2) ret *= a;
    a = a * a;
    n /= 2;
  }
  return ret;
}
const int MAXN = 2e3 + 1, INF = 0x3fffffff;
int n;
string a, b;
void input() {
  cin >> n;
  a.resize(n), b.resize(n);
  for (int(i) = (0); (i) < (int)(n); ++(i)) cin >> a[i] >> b[i];
}
map<string, int> dp[MAXN][10];
int f(int p, int c, const string& s) {
  if (p == n && s == "0000") return 0;
  int& ret = dp[p][c][s];
  if (ret) return ret;
  ret = INF;
  for (char x = '1'; x <= '9'; ++x) {
    string ts = s;
    int cnt = 0;
    for (int(i) = (0); (i) < (int)(4); ++(i)) {
      if (ts[i] == x) ++cnt, ts[i] = '0';
    }
    int np = p;
    for (int(i) = (0); (i) < (int)(4); ++(i)) {
      if (ts[i] == '0' && a[np] == x) ++cnt, ts[i] = b[np++];
    }
    sort(ts.begin(), ts.end());
    if (np == p && ts == s) continue;
    ret = min(ret, f(np, x - '0', ts) + cnt + abs(x - '0' - c));
  }
  return ret;
}
int solve() {
  cout << f(0, 1, "0000") << '\n';
  return 0;
}
void execute() { input(), solve(); }
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  execute();
  return 0;
}
