#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 = 2001, MAXS = 1100;
int n, a[MAXN], b[MAXN], dp[MAXN][MAXS];
void input() {
  cin >> n;
  for (int(i) = (0); (i) < (int)(n); ++(i)) cin >> a[i] >> b[i];
}
vector<int> get(int s) {
  vector<int> ret;
  for (int(i) = (0); (i) < (int)(4); ++(i)) {
    int c = s % 10;
    if (c) ret.push_back(c);
    s /= 10;
  }
  return ret;
}
int get2(vector<int>& s) {
  int ret = 0;
  for (int(i) = ((int)s.size() - 1); (i) >= (int)(0); --(i)) {
    ret *= 10;
    ret += s[i];
  }
  return ret;
}
vector<int> push(int s, int x) {
  vector<int> st = get(s);
  for (int(i) = (0); (i) < (int)(st.size()); ++(i)) {
    if (st[i] >= x) {
      st.insert(st.begin() + i, x);
      return st;
    }
  }
  st.push_back(x);
  return st;
}
int calcFinal(int s) {
  vector<int> st = push(s, b[n - 1]);
  int ret = st.size();
  return st.size() +
         min(abs(a[n - 1] - st.front()), abs(a[n - 1] - st.back())) +
         st.back() - st.front();
}
int f(int p, int s) {
  int& ret = dp[p][s];
  if (ret != -1) return ret;
  if (p == n - 1) {
    return ret = calcFinal(s);
  }
  vector<int> st = get(s);
  int ans = 0xfffffff;
  if (st.size() < 3) {
    vector<int> nxt = push(s, b[p]);
    ans = min(ans, f(p + 1, get2(nxt)) + abs(a[p] - a[p + 1]));
  }
  st = push(s, b[p]);
  for (int(i) = (0); (i) < (int)(st.size()); ++(i)) {
    if (i && st[i] == st[i - 1]) continue;
    for (int(j) = (i); (j) < (int)(st.size()); ++(j)) {
      int pls = (j - i + 1) +
                min(abs(a[p] - st[i]) + abs(a[p + 1] - st[j]),
                    abs(a[p] - st[j]) + abs(a[p + 1] - st[i])) +
                st[j] - st[i];
      vector<int> nxt = st;
      nxt.erase(nxt.begin() + i, nxt.begin() + j + 1);
      ans = min(ans, f(p + 1, get2(nxt)) + pls);
    }
  }
  return ret = ans + 1;
}
int solve() {
  memset(dp, -1, sizeof(dp));
  cout << f(0, 0) + a[0] << '\n';
  return 0;
}
void execute() { input(), solve(); }
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  execute();
  return 0;
}
