#include <bits/stdc++.h>
using namespace std;
const int N = 2010, H = 9, C = 4, INF = 1e8;
struct state {
  int sat, hgt;
  vector<int> todo;
  bool operator<(const state& u) const {
    return sat != u.sat   ? sat < u.sat
           : hgt != u.hgt ? hgt < u.hgt
                          : todo < u.todo;
  }
  bool operator==(const state& u) const {
    return sat == u.sat && hgt == u.hgt && todo == u.todo;
  }
  void add(int h) {
    todo.push_back(h);
    sort(todo.begin(), todo.end());
    sat++;
  }
  void rmv(int h) {
    vector<int> nt;
    for (int i = 0; i < todo.size(); i++) {
      if (todo[i] != h) {
        nt.push_back(todo[i]);
      }
    }
    todo.swap(nt);
  }
};
int n, a[N], b[N], ans = INF;
map<state, int> dist;
map<state, bool> vis;
priority_queue<pair<int, state>> q;
void pr(state u) {
  cout << "dst = " << dist[u] << ", ";
  cout << "sat = " << u.sat << ", ";
  cout << "hgt = " << u.hgt << endl;
  for (int x : u.todo) {
    cout << x << " ";
  }
  cout << endl;
}
void walk(state u, state v, int del) {
  if (dist[v] == 0 || dist[u] + del < dist[v]) {
    dist[v] = dist[u] + del;
    q.emplace(-dist[v], v);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  state s = state();
  dist[s] = 1;
  q.emplace(-1, s);
  while (!q.empty()) {
    state u = q.top().second;
    q.pop();
    if (vis[u]) {
      continue;
    } else if (u.sat == n && u.todo.empty()) {
      ans = min(ans, dist[u]);
    }
    vis[u] = true;
    if (u.sat < n) {
      if (u.todo.size() < C && u.hgt == a[u.sat]) {
        state v = u;
        v.add(b[u.sat]);
        walk(u, v, 0);
        continue;
      }
    }
    if (find(u.todo.begin(), u.todo.end(), u.hgt) != u.todo.end()) {
      state v = u;
      v.rmv(u.hgt);
      walk(u, v, 0);
      continue;
    }
    if (u.hgt != 0) {
      state v = u;
      v.hgt--;
      walk(u, v, 1);
    }
    if (u.hgt != H - 1) {
      state v = u;
      v.hgt++;
      walk(u, v, 1);
    }
  }
  cout << (ans - 1) + 2 * n << endl;
}
