#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
 
 
#define puba push_back
#define mapa make_pair
#define ff first
#define ss second
#define pii pair < LL, LL > 
 
 
using namespace std;
 
 
typedef long long LL;
 
 
const LL INF = (LL) 1e18;
 
typedef struct {
    LL l, r, d;   
} TSeg;
 
 
void get_dif(vector < LL > &ans, vector < LL > &a) {
    ans.resize(a.size());
    for (LL i = 0; i < (LL) a.size(); ++i) {
        ans[i] = a[i];
    }
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
}
 
 
LL find_pos(vector < TSeg > &seg, LL brd) {
    //TSeg tmp; tmp.l = INF + 1000;
    //seg.puba(tmp);
    LL l = 0;
    LL r = seg.size();
    for (; r - l > 1; ) {
        LL m = (r + l) / 2;
        if (seg[m].l > brd) r = m;
        else l = m;
    }
 
    //seg.pop_back();
    return l;
}
 
 
LL get_ans(vector < LL > &f, vector < LL > &sum, LL val) {
    LL num_less = lower_bound(f.begin(), f.end(), val) - f.begin();
 
    return val * num_less - sum[num_less] + sum[f.size()] - sum[num_less] - val * ((LL) f.size() - num_less);  
}
 
 
int main() {
    freopen("sequence.in", "r", stdin);
    freopen("sequence.out", "w", stdout);
 
    LL n;
    scanf("%I64d", &n);
    vector < LL > b (n);
    for (LL i = 0; i < n; ++i) {
        scanf("%I64d", &b[i]);
    }
 
    vector < LL > fa, fb, fab;
    for (LL i = 0; i < n; ++i) {
        if (i % 6 == 0) fa.puba(-b[i]);
        if (i % 6 == 1) fb.puba(b[i]);
        if (i % 6 == 2) fab.puba(b[i]);
        if (i % 6 == 3) fa.puba(b[i]);
        if (i % 6 == 4) fb.puba(-b[i]);
        if (i % 6 == 5) fab.puba(-b[i]);
    }
 
 
    sort(fa.begin(), fa.end());
    sort(fb.begin(), fb.end());
    sort(fab.begin(), fab.end());
     
    
    vector < LL > ua, ub, uab;
    get_dif(ua, fa);
    get_dif(ub, fb);
    get_dif(uab, fab);
 
    // for -a
 
    vector < LL > suma (fa.size() + 1);
    suma[0] = 0;
    for (LL i = 0; i < (LL) fa.size(); ++i) {
        suma[i + 1] = suma[i] + fa[i];
    }
 
    vector < LL > sumb (fb.size() + 1);
    sumb[0] = 0;
    for (LL i = 0; i < (LL) fb.size(); ++i) {
        sumb[i + 1] = sumb[i] + fb[i];
    }
 
 
    vector < LL > sumab (fab.size() + 1);
    sumab[0] = 0;
    for (LL i = 0; i < (LL) fab.size(); ++i) {
        sumab[i + 1] = sumab[i] + fab[i];
    }
 
 
    vector < TSeg > sega (ua.size() + 1);
 
    for (LL i = 0; i < (LL) ua.size(); ++i) {
        if (i == 0) {
            sega[i].l = -INF;
            sega[i].d = -((LL) fa.size());
        }
        if (i + 1 == (LL) ua.size()) {
            sega[i + 1].r = INF;
        }
 
        sega[i].r = ua[i];
        sega[i + 1].l = ua[i] + 1;
        sega[i + 1].d = sega[i].d + 2 * (upper_bound(fa.begin(), fa.end(), ua[i]) - lower_bound(fa.begin(), fa.end(), ua[i]));
    }
 
    vector < TSeg > segb (ub.size() + 1);
 
    for (LL i = 0; i < (LL) ub.size(); ++i) {
        if (i == 0) {
            segb[i].l = -INF;
            segb[i].d = -((LL) fb.size());
        }
        if (i + 1 == (LL) ub.size()) {
            segb[i + 1].r = INF;
        }
 
        segb[i].r = ub[i];
        segb[i + 1].l = ub[i] + 1;
        segb[i + 1].d = segb[i].d + 2 * (upper_bound(fb.begin(), fb.end(), ub[i]) - lower_bound(fb.begin(), fb.end(), ub[i]));
    }
 
    vector < TSeg > segab (uab.size() + 1);
 
    for (LL i = 0; i < (LL) uab.size(); ++i) {
        if (i == 0) {
            segab[i].l = -INF;
            segab[i].d = -((LL) fab.size());
        }
        if (i + 1 == (LL) uab.size()) {
            segab[i + 1].r = INF;
        }
 
        segab[i].r = uab[i];
        segab[i + 1].l = uab[i] + 1;
        segab[i + 1].d = segab[i].d + 2 * (upper_bound(fab.begin(), fab.end(), uab[i]) - lower_bound(fab.begin(), fab.end(), uab[i]));
    }
    /*
 
    for (LL i = 0; i < (LL) sega.size(); ++i) {
        cout << sega[i].l << " " << sega[i].r << " " << sega[i].d << endl;
    }
    cout << endl;
 
    for (LL i = 0; i < (LL) segb.size(); ++i) {
        cout << segb[i].l << " " << segb[i].r << " " << segb[i].d << endl;
    }
    cout << endl;
 
    for (LL i = 0; i < (LL) segab.size(); ++i) {
        cout << segab[i].l << " " << segab[i].r << " " << segab[i].d << endl;
    }
    cout << endl;
    */
 
    // for -a
 
    LL ga = 0, gb = 0, gab = 0;
    LL gans = INF;
    for (LL i = 0; i < (LL) ua.size(); ++i) {
        LL ta = ua[i];
         
         
        LL lb = -INF;
        LL rb = INF;
 
        for (; rb - lb > 1; ) {
            LL mbb = (lb + rb) / 2;
            LL mbab = mbb + ta;
             
            LL pb = find_pos(segb, mbb);
            LL pab = find_pos(segab, mbab);
            /*
            cout << pb << " " << mbb << endl;
            cout << pab << " " << mbab << endl;
            cout << endl;
            */
            if (segb[pb].d + segab[pab].d <= 0) {
                lb = mbb;
            }
            else {
                rb = mbb;
            }
        }
        LL tb = lb;
        LL tab = ta + tb;
         
        LL tmp = get_ans(fa, suma, ta) + get_ans(fb, sumb, tb) + get_ans(fab, sumab, tab);
        if (tmp < gans) {
            gans = tmp;
            ga = ta;
            gb = tb;
            gab = tab;
        }
        //cout << ta << " " << tb << " " << tab << " " << tmp << endl;  
         
    }
 
 
    for (LL i = 0; i < (LL) ub.size(); ++i) {
        LL tb = ub[i];
         
         
        LL la = -INF;
        LL ra = INF;
 
        for (; ra - la > 1; ) {
            LL mba = (la + ra) / 2;
            LL mbab = mba + tb;
             
            LL pa = find_pos(sega, mba);
            LL pab = find_pos(segab, mbab);
            /*
            cout << pa << " " << mba << endl;
            cout << pab << " " << mbab << endl;
            cout << endl;
            */
            if (sega[pa].d + segab[pab].d <= 0) {
                la = mba;
            }
            else {
                ra = mba;
            }
        }
        LL ta = la;
        LL tab = ta + tb;
         
        LL tmp = get_ans(fa, suma, ta) + get_ans(fb, sumb, tb) + get_ans(fab, sumab, tab);
        if (tmp < gans) {
            gans = tmp;
            ga = ta;
            gb = tb;
            gab = tab;
        }
        //cout << ta << " " << tb << " " << tab << " " << tmp << endl;  
         
    }
 
    printf("%I64d\n", gans);
 
    return 0;
}
