#include <bits/stdc++.h>
using namespace std;
void init();
void solve();
int main() {
  try {
    init();
    solve();
  } catch (...) {
    return 0;
  }
  return 0;
}
template <class T>
inline bool checkMin(T& a, T b) {
  return (a > b ? a = b, 1 : 0);
}
template <class T>
inline bool checkMax(T& a, T b) {
  return (a < b ? a = b, 1 : 0);
}
void ALERT(bool judgememt, const char* phrase) {
  if (judgememt) {
    puts(phrase);
    throw "ALERT";
  }
}
bool alert(bool judgememt, const char* phrase) {
  if (judgememt) puts(phrase);
  return judgememt;
}
class Scanner {
 private:
  static const int BUFFER_SIZE = 10000;
  char buff[BUFFER_SIZE];
  int buffPos, buffLim;

 public:
  Scanner() {
    buffLim = fread(buff, 1, BUFFER_SIZE, stdin);
    buffPos = 0;
  }

 private:
  inline void flushBuff() {
    buffLim = fread(buff, 1, BUFFER_SIZE, stdin);
    if (buffLim == 0) {
      buff[buffLim++] = '\n';
    }
    buffPos = 0;
  }
  inline bool isWS(char t) { return t == ' ' || t == '\n'; }
  inline bool isDig(char t) { return t >= '0' && t <= '9'; }
  void nextPos() {
    buffPos++;
    if (buffPos == buffLim) {
      flushBuff();
    }
  }

 public:
  inline char getchar() {
    char ch = buff[buffPos];
    nextPos();
    return ch;
  }
  inline void next(char* s) {
    while (isWS(buff[buffPos])) {
      nextPos();
    }
    while (!isWS(buff[buffPos])) {
      *s = buff[buffPos];
      s++;
      nextPos();
    }
    *s = '\0';
  }
  inline void nextLine(char* s) {
    while (buff[buffPos] != '\n') {
      nextPos();
    }
    if (buff[buffPos] == '\n') {
      nextPos();
    }
    while (buff[buffPos] != '\n') {
      *s = buff[buffPos];
      s++;
      buffPos++;
    }
    *s = '\0';
  }
  inline int nextInt() {
    while (!isDig(buff[buffPos])) {
      nextPos();
    }
    int sign = (buff[buffPos] == '-') ? nextPos(), -1 : 1;
    int res = 0;
    while (isDig(buff[buffPos])) {
      res = res * 10 + buff[buffPos] - '0';
      nextPos();
    }
    return res * sign;
  }
  inline double nextDouble() {
    while (isWS(buff[buffPos])) {
      nextPos();
    }
    int sign = (buff[buffPos] == '-') ? nextPos(), -1 : 1;
    double res = 0;
    while (isDig(buff[buffPos])) {
      res = res * 10 + buff[buffPos] - '0';
      nextPos();
    }
    if (buff[buffPos] == '.') {
      nextPos();
      double ep = 1;
      while (isDig(buff[buffPos])) {
        ep *= 0.1;
        res += ep * (buff[buffPos] - '0');
        nextPos();
      }
    }
    return sign * res;
  }
  inline char nextChar() {
    while (isWS(buff[buffPos])) nextPos();
    char res = buff[buffPos];
    nextPos();
    return res;
  }
};
class Intm {
 private:
  static unsigned long long MOD;
  static unsigned long long k;
  unsigned long long a;

 public:
  inline static void setMod(const unsigned long long _MOD) {
    MOD = _MOD;
    k = (__uint128_t(1) << 64) / _MOD;
  }
  inline static void getModSub(unsigned long long& a) {
    if (a >= MOD) a -= MOD;
  }
  inline static void getModAdd(unsigned long long& a) {
    if (a < 0) a += MOD;
  }
  inline static void getMod(unsigned long long& a) {
    unsigned long long q = (unsigned long long)((__uint128_t(k) * a) >> 64);
    a -= q * MOD;
    getModSub(a);
  }
  inline static unsigned long long power(unsigned long long a,
                                         unsigned long long b) {
    unsigned long long r = 1;
    while (b) {
      if (b & 1) getMod(r *= a);
      getMod(a *= a);
      b >>= 1;
    }
    return r;
  }
  Intm() { a = 0; }
  Intm(unsigned long long _a) : a(_a) {}
  Intm& operator+=(const Intm b) {
    a += b.a;
    getModSub(a);
    return *this;
  }
  Intm operator+(const Intm b) const {
    Intm second(a);
    return (second += b);
  }
  Intm& operator-=(const Intm b) {
    a -= b.a;
    getModAdd(a);
    return *this;
  }
  Intm operator-(const Intm b) const {
    Intm second(a);
    return (second -= b);
  }
  Intm& operator*=(const Intm b) {
    a *= b.a;
    getMod(a);
    return *this;
  }
  Intm operator*(const Intm b) const {
    Intm second(a);
    return (second *= b);
  }
  Intm& operator/=(const Intm b) {
    a *= power(b.a, MOD - 2);
    getMod(a);
    return *this;
  }
  Intm operator/(const Intm b) const {
    Intm second(a);
    return (second /= b);
  }
  operator long long() { return (long long)a; }
};
unsigned long long Intm::MOD;
unsigned long long Intm::k;
const int N = 2005;
const int K = 11;
map<string, int> dp[N][K];
int a[N], b[N];
int n, k;
int _abs(int u) { return u > 0 ? u : -u; }
int dfs(int st, int pos, string stat) {
  if (st >= n && stat == "0000") return 0;
  int& ans = dp[st][pos][stat];
  if (ans) return ans;
  ans = 0x3f3f3f3f;
  for (int i = 1; i <= 10; i++) {
    int tim = 0, vst = st;
    string vstat = stat;
    for (int j = 0; j < 4; j++)
      if (vstat[j] == i + '0') {
        tim++;
        vstat[j] = '0';
      }
    for (int j = 0; j < 4; j++)
      if (vstat[j] == '0' && a[vst] == i) {
        tim++;
        vstat[j] = '0' + b[vst];
        vst++;
      }
    sort(vstat.begin(), vstat.end());
    if (tim != 0) checkMin(ans, tim + _abs(i - pos) + dfs(vst, i, vstat));
  }
  return ans;
}
void init() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]);
}
void solve() { printf("%d\n", dfs(0, 1, "0000")); }
