#include <bits/stdc++.h>   class state {    public:     int d[4];     int e;     state() : d{0, 0, 0, 0}, e(1) {}     bool operator<(const state &x) const {       for (int i = 0; i < 4; ++i) {         if (d[i] == x.d[i]) continue;         return d[i] > x.d[i];       }       return e > x.e;     }     bool operator!=(const state &x) const {       for (int i = 0; i < 4; ++i)         if (d[i] != x.d[i]) return true;       return e != x.e;     }   };   std::map<state, int> mp;   int main() {     int n;     scanf( %d , &n);     mp[state()] = 0;     while (n--) {       int a, b;       scanf( %d%d , &a, &b);       std::map<state, int> nmp;       for (auto it = mp.begin(); it != mp.end(); ++it) {         if (it->first.d[0] != 0) continue;         state s = it->first;         int t = it->second;         t += abs(a - s.e) + 1;         s.e = a;         s.d[0] = b;         std::sort(s.d, s.d + 4);         auto it2 = nmp.lower_bound(s);         if (it2 == nmp.end() || it2->first != s)           it2 = nmp.insert(it2, std::pair<state, int>(s, t));         it2->second = std::min(it2->second, t);       }       mp.swap(nmp);       0;       for (auto it = mp.begin(); it != mp.end(); ++it) {         0;         for (int i = 0; i < 4; ++i) {           if (!it->first.d[i]) continue;           state s = it->first;           int t = it->second;           t += abs(s.d[i] - s.e) + 1;           s.e = s.d[i];           s.d[i] = 0;           std::sort(s.d, s.d + 4);           auto it2 = mp.lower_bound(s);           if (it2 == mp.end() || it2->first != s)             it2 = mp.insert(it2, std::pair<state, int>(s, t));           it2->second = std::min(it2->second, t);         }       }     }     int ans = ((int)1e9);     for (auto it = mp.begin(); it != mp.end(); ++it) {       if (it->first.d[0] || it->first.d[1] || it->first.d[2] || it->first.d[3])         continue;       ans = std::min(ans, it->second);     }     printf( %d n , ans);     return 0;   }   