#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vll>;
using vb = vector<bool>;
const int INF = 2e9 + 5;
const ll LLINF = (1LL << 60) + 5;
int main() {
  cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0);
  int n;
  cin >> n;
  vector<pii> v(n);
  for (pii &p : v) {
    cin >> p.first >> p.second;
  }
  int count = 0;
  vi code(1e4, -1), decode(1e3, -1), folks(1e3, 0);
  vvi vals(1e3);
  for (int i = 0; i <= 9; i++)
    for (int j = i; j <= 9; j++)
      for (int k = j; k <= 9; k++)
        for (int l = k; l <= 9; l++) {
          int val = i * 1000 + j * 100 + k * 10 + l;
          assert(code[val] == -1);
          code[val] = count++;
          decode[code[val]] = val;
          folks[code[val]] = (i != 0) + (j != 0) + (k != 0) + (l != 0);
          if (i) vals[code[val]].push_back(i);
          if (j) vals[code[val]].push_back(j);
          if (k) vals[code[val]].push_back(k);
          if (l) vals[code[val]].push_back(l);
        }
  auto reconstruct = [&](vi &v) {
    int val = 0;
    for (int i = 0; i < v.size(); i++) val += v[i] * pow(10, v.size() - 1 - i);
    return code[val];
  };
  auto add_to_state = [&](int state, int pos) {
    vi v = vals[state];
    assert(v.size() != 4);
    v.push_back(pos);
    sort(v.begin(), v.end());
    return reconstruct(v);
  };
  auto remove_from_state = [&](int state, int pos) {
    vi x;
    for (int i : vals[state])
      if (i != pos) x.push_back(i);
    return reconstruct(x);
  };
  vector<vvi> memo(n + 1, vvi(9, vi(count, -1)));
  function<int(int, int, int)> dp = [&](int i, int pos, int state) {
    if (i == n && vals[state].empty()) return 0;
    int &ans = memo[i][pos - 1][state];
    if (ans != -1) return ans;
    ans = INF;
    if (i < n && vals[state].size() < 4) {
      ans =
          min(ans, abs(pos - v[i].first) +
                       dp(i + 1, v[i].first, add_to_state(state, v[i].second)));
    }
    for (int j : vals[state]) {
      ans = min(ans, abs(pos - j) + dp(i, j, remove_from_state(state, j)));
    }
    return ans;
  };
  cout << dp(0, 1, code[0]) + 2 * n;
  return cout << endl, 0;
}
