#include <bits/stdc++.h>
using namespace std;
const int MAX_STATE = 300000000, MAX_ANS = 1000000;
int num(array<int, 7>& state) {
  sort(state.begin(), state.begin() + 4);
  int pow = 1, result = 0;
  for (int i = 0; i < 6; ++i) {
    result += pow * state[i];
    pow *= 10;
  }
  return result;
}
void add(array<int, 7>& ns, queue<array<int, 7>>& q, bitset<MAX_STATE>& used) {
  ++ns[6];
  int ns_num = num(ns);
  if (!used[ns_num]) {
    q.push(ns);
    used[ns_num] = true;
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  int answer = MAX_ANS;
  int n;
  cin >> n;
  vector<array<int, 2>> ab(n);
  for (int i = 0; i < n; ++i) cin >> ab[i][0] >> ab[i][1];
  queue<array<int, 7>> q;
  auto* used_ptr = new bitset<MAX_STATE>;
  bitset<MAX_STATE>& used = *used_ptr;
  array<int, 7> fs = {0, 0, 0, 0, 1, 0, 0};
  q.push(fs);
  used[num(fs)] = true;
  while (!q.empty()) {
    array<int, 7> s = q.front();
    q.pop();
    if (s[3] == 0 && s[5] == n) {
      answer = min(answer, s[6]);
    } else {
      for (int i = 0; i < 4; ++i) {
        if (s[i] == s[4]) {
          array<int, 7> ns = s;
          ns[i] = 0;
          add(ns, q, used);
        }
      }
      if (ab[s[5]][0] == s[4] && s[0] == 0) {
        array<int, 7> ns = s;
        ns[0] = ab[s[5]][1];
        ++ns[5];
        add(ns, q, used);
      }
      if (s[4] < 9) {
        array<int, 7> ns = s;
        ++ns[4];
        add(ns, q, used);
      }
      if (s[4] > 1) {
        array<int, 7> ns = s;
        --ns[4];
        add(ns, q, used);
      }
    }
  }
  cout << answer;
  delete used_ptr;
  return 0;
}
