#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = vector<vector<T>>;
constexpr ll mod = 1000000007;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 + 99);
const vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
template <typename T, typename U>
inline bool chmin(T& t, const U& u) {
  if (t > u) {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T, typename U>
inline bool chmax(T& t, const U& u) {
  if (t < u) {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T>
inline T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T, typename Y>
inline T mpow(T a, Y n) {
  T res = 1;
  for (; n; n >>= 1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}
template <typename T, typename Y>
ostream& operator<<(ostream& os, const pair<T, Y>& p) {
  return os << "{" << p.first << "," << p.second << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const V<T>& v) {
  os << "{";
  for (auto e : v) os << e << ",";
  return os << "}";
}
template <typename... Args>
void debug(Args&... args) {
  for (auto const& x : {args...}) {
    cerr << x << ' ';
  }
  cerr << '\n';
}
V<int> tmp(4);
inline int getnex_out(int sta, int des) {
  int res = 0;
  for (ll i = 0; i < (ll)(4); ++i) {
    if (sta % 10 != des)
      tmp[i] = sta % 10;
    else
      tmp[i] = 0;
    sta /= 10;
  }
  sort((tmp).begin(), (tmp).end());
  for (ll i = 0; i < (ll)(4); ++i) {
    res += tmp[i] * mpow(10, 3 - i);
  }
  return res;
}
V<int> a, b;
inline int getnex_in(int sta, int des) {
  int res = 0;
  for (ll i = 0; i < (ll)(4); ++i) {
    tmp[i] = sta % 10;
    sta /= 10;
  }
  tmp[3] = des;
  sort((tmp).begin(), (tmp).end());
  for (ll i = 0; i < (ll)(4); ++i) {
    res += tmp[i] * mpow(10, 3 - i);
  }
  return res;
}
int dp[2010][1000][10] = {};
V<int> se(0);
inline int getind(int x) {
  return distance(se.begin(), lower_bound((se).begin(), (se).end(), x));
}
signed main() {
  cin.tie(0);
  cerr.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a.resize(n), b.resize(n);
  for (ll i = 0; i < (ll)(n); ++i) cin >> a[i] >> b[i];
  for (ll i = 0; i < (ll)(2010); ++i)
    for (ll j = 0; j < (ll)(1000); ++j)
      for (ll k = 0; k < (ll)(10); ++k) dp[i][j][k] = INF;
  dp[0][0][1] = 0;
  queue<int> q;
  for (ll i = 1; i < (ll)(10); ++i) q.push(i);
  while (q.size()) {
    int now = q.front();
    q.pop();
    se.push_back(now);
    if (now < 1000) {
      for (ll i = now % 10; i < (ll)(10); ++i) {
        q.push(10 * now + i);
      }
    }
  }
  se.push_back(0);
  sort((se).begin(), (se).end());
  for (ll i = 0; i < (ll)(n + 1); ++i) {
    for (ll j = (ll)(se).size() - 1; (ll)0 <= j; --j) {
      for (ll f = 1; f < (ll)(10); ++f) {
        if (dp[i][j][f] == INF) continue;
        int sta = se[j], cnt = 0;
        for (ll k = 1; k < (ll)(10); ++k) {
          sta = se[j], cnt = 0;
          for (ll l = 0; l < (ll)(4); ++l) {
            if (sta % 10 == k) cnt++;
            sta /= 10;
          }
          if (cnt == 0) continue;
          chmin(dp[i][getind(getnex_out(se[j], k))][k],
                dp[i][j][f] + cnt + abs(f - k));
        }
        if (se[j] >= 1000 || i >= n) continue;
        chmin(dp[i + 1][getind(getnex_in(se[j], b[i]))][a[i]],
              dp[i][j][f] + abs(f - a[i]) + 1);
      }
    }
  }
  int ans = INF;
  for (ll i = 1; i < (ll)(10); ++i) chmin(ans, dp[n][0][i]);
  cout << ans << '\n';
}
