#include <bits/stdc++.h>
constexpr int MAX = 2e3 + 1;
std::map<std::multiset<int>, int> mem[10][2001];
int A[MAX], B[MAX];
int n, a, b;
int dp(std::multiset<int>& ms, const int floor, const int idx) {
  if (idx == n + 1) {
    if (ms.empty())
      return 0;
    else
      return std::min(std::abs(*ms.begin() - floor),
                      std::abs(*ms.rbegin() - floor)) +
             (*ms.rbegin() - *ms.begin()) + ms.size();
  }
  int& ans = (mem[floor][idx])[ms];
  if (ans)
    return ans;
  else
    ans = std::numeric_limits<int>::max();
  if (ms.size() < 4) {
    ms.insert(B[idx]);
    ans = dp(ms, A[idx], idx + 1) + 1 + std::abs(A[idx] - floor);
    ms.erase(ms.find(B[idx]));
  }
  if (ms.empty()) return ans;
  auto it = ms.lower_bound(floor);
  const int lower = *it;
  if (it != ms.begin()) {
    auto it_ = it;
    --it_;
    const int lower_ = *it_;
    ms.erase(it_);
    ans = std::min(dp(ms, lower_, idx) + 1 + std::abs(floor - lower_), ans);
    ms.insert(lower_);
  }
  if (it != ms.end()) {
    ms.erase(ms.find(lower));
    ans = std::min(dp(ms, lower, idx) + 1 + std::abs(floor - lower), ans);
    ms.insert(lower);
  }
  return ans;
}
int main(void) {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) std::cin >> A[i] >> B[i];
  std::multiset<int> ms;
  std::cout << dp(ms, 1, 1) << '\n';
  return 0;
}
