#include <bits/stdc++.h>
using namespace std;
int n;
int fr[2011], to[2011];
int f[2][747][10];
int t_id[11111], val[747], sz[747], match[747][11], dig[747][4];
bool can_board(int id, int dest) {
  if (sz[id] < 4) return true;
  if (match[id][dest]) return true;
  return false;
}
void upd(int &a, int b) {
  if (b < 0) return;
  if (a == -1 || a > b) a = b;
}
pair<int, int> INC[747][11], DEC[747][11];
pair<int, int> _inc(int id, int v) {
  if (INC[id][v].first >= 0) return INC[id][v];
  pair<int, int> &t = INC[id][v];
  int a[4];
  for (int i = 0; i < 4; i++) a[i] = dig[id][i];
  sort(a, a + 4);
  a[0] = v;
  sort(a, a + 4);
  int x = 0;
  for (int i = 3; i >= 0; i--) x = x * 10 + a[i];
  t = make_pair(t_id[x], 1);
  return t;
}
pair<int, int> _dec(int id, int v) {
  if (DEC[id][v].first >= 0) return DEC[id][v];
  pair<int, int> &t = DEC[id][v];
  int cost = 0;
  int a[4];
  for (int i = 0; i < 4; i++) {
    a[i] = dig[id][i];
    if (a[i] == v) {
      a[i] = 0;
      cost++;
    }
  }
  sort(a, a + 4);
  int x = 0;
  for (int i = 3; i >= 0; i--) x = x * 10 + a[i];
  t = make_pair(t_id[x], cost);
  return t;
}
pair<int, int> pick(int id, int pos, int fr, int to) {
  pair<int, int> res = make_pair(0, labs(pos - fr));
  pair<int, int> x = _dec(id, fr);
  res.second += x.second;
  x = _inc(x.first, to);
  res.second += x.second;
  res.first = x.first;
  return res;
}
pair<int, int> drop(int id, int pos, int to) {
  pair<int, int> res = make_pair(0, labs(pos - to));
  pair<int, int> x = _dec(id, to);
  res.first = x.first;
  res.second += x.second;
  return res;
}
int main() {
  int cnt = 0, a[4];
  for (int i = 0; i < 10000; i++) {
    int x = i, cap = 0;
    for (int j = 0; j < 4; j++) {
      a[j] = x % 10;
      x /= 10;
      if (a[j] > 0) cap++;
    }
    bool chk = true;
    for (int j = 3; j > 0; j--)
      if (a[j] < a[j - 1]) chk = false;
    if (chk) {
      cnt++;
      t_id[i] = cnt;
      val[cnt] = i;
      sz[cnt] = cap;
      for (int j = 0; j < 4; j++) {
        match[cnt][a[j]] = true;
        dig[cnt][j] = a[j];
      }
    }
  }
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &fr[i], &to[i]);
  fr[n + 1] = -1;
  for (int i = 0; i < 747; i++)
    for (int j = 0; j < 11; j++) INC[i][j] = DEC[i][j] = make_pair(-1, -1);
  int cur = 0, nxt = 1;
  memset(f, -1, sizeof f);
  f[0][1][1] = 0;
  for (int i = 1; i <= n; i++) {
    memset(f[nxt], -1, sizeof f[nxt]);
    int dest = fr[i];
    for (int id = 1; id <= 715; id++)
      for (int pos = 1; pos <= 9; pos++)
        if (f[cur][id][pos] >= 0 && (can_board(id, dest))) {
          pair<int, int> p = pick(id, pos, dest, to[i]);
          upd(f[nxt][p.first][dest], f[cur][id][pos] + p.second);
        }
    for (int id = 715; id > 0; id--)
      for (int pos = 1; pos <= 9; pos++) {
        if (f[nxt][id][pos] < 0) continue;
        for (int j = 0; j < 4; j++) {
          int todo = dig[id][j];
          if (todo < 1) continue;
          pair<int, int> p = drop(id, pos, todo);
          upd(f[nxt][p.first][todo], f[nxt][id][pos] + p.second);
        }
      }
    swap(cur, nxt);
  }
  int ans = -1;
  for (int i = 1; i <= 9; i++) upd(ans, f[cur][1][i]);
  printf("%d\n", ans);
  return 0;
}
