#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  using P = array<int, 7>;
  static bitset<2001> vis[10][10][10][10][10];
  int N;
  cin >> N;
  vi A(N), B(N);
  for (int(a) = (0); (a) < (int)(N); ++(a)) cin >> A[a] >> B[a];
  queue<P> Q;
  Q.push({0, 0, 0, 0, 1, 0, 0});
  while ((int)(Q).size()) {
    auto p = Q.front();
    Q.pop();
    auto &v = vis[p[0]][p[1]][p[2]][p[3]][p[4]];
    if (v[p[5]]) continue;
    int d = p[6]++, i = p[5], cur = p[4];
    v[p[5]] = 1;
    if (p[3] == 0 && i == N) {
      cout << d << endl;
      break;
    }
    if (i < N && A[i] == cur && p[0] == 0) {
      auto np = p;
      np[0] = B[i];
      np[5]++;
      sort(np.begin(), np.begin() + 4);
      Q.push(np);
    }
    if (cur > 1) {
      p[4]--;
      Q.push(p);
      p[4]++;
    }
    if (cur < 9) {
      p[4]++;
      Q.push(p);
      p[4]--;
    }
    assert(cur == p[4]);
    for (int(a) = (0); (a) < (int)(4); ++(a))
      if (p[a] == cur) {
        p[a] = 0;
        sort(p.begin(), p.begin() + 4);
        Q.push(p);
        break;
      }
  }
}
