#include <bits/stdc++.h>
using namespace std;
const int mn = 2005;
int a[mn];
int n;
int d[mn][10 * 10 * 10 + 3][10];
int l[mn], r[mn];
struct node {
  int u, S, p;
};
int b[4];
inline void rzip(int S, int b[]) {
  b[0] = S % 10;
  S /= 10;
  b[1] = S % 10;
  S /= 10;
  b[2] = S % 10;
}
inline int zip(int c[]) {
  int b[4];
  for (int i = 0; i < 3; i++) b[i] = c[i];
  sort(b, b + 3);
  int s = 0;
  for (int i = 0; i < 3; i++) {
    s *= 10;
    s += b[i];
  }
  return s;
}
int dfs(int u, int S, int p) {
  int &ans = d[u][S][p];
  if (ans != -1) return ans;
  if (u == n + 1 && S == 0) return ans = 0;
  int b[4];
  rzip(S, b);
  sort(b, b + 3);
  ans = 1 << 30;
  if (b[0] == p || b[1] == p || b[2] == p) {
    int v = 0;
    for (int i = 0; i < 3; i++) {
      if (b[i] == p) b[i] = 0, v++;
    }
    ans = min(dfs(u, zip(b), p) + v, ans);
  } else if (l[u] == p) {
    if (b[0] == 0) {
      b[0] = r[u];
      ans = min(dfs(u + 1, zip(b), p) + 1, ans);
    } else {
      int v = b[0];
      b[0] = r[u];
      ans = min(dfs(u + 1, zip(b), v) + abs(p - v) + 2, ans);
      b[0] = v;
      v = b[1];
      b[1] = r[u];
      ans = min(dfs(u + 1, zip(b), v) + abs(p - v) + 2, ans);
      b[1] = v;
      v = b[2];
      b[2] = r[u];
      ans = min(dfs(u + 1, zip(b), v) + abs(p - v) + 2, ans);
      b[2] = v;
      v = r[u];
      ans = min(dfs(u + 1, zip(b), v) + abs(p - v) + 2, ans);
    }
  } else {
    sort(b, b + 3);
    for (int i = 0; i < 3; i++)
      if (b[i]) {
        ans = min(ans, dfs(u, zip(b), b[i]) + abs(b[i] - p));
      }
    if (u != n + 1) ans = min(ans, dfs(u, zip(b), l[u]) + abs(l[u] - p));
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
  }
  memset(d, -1, sizeof(d));
  cout << dfs(1, 0, 1);
  return 0;
}
