#include "bits/stdc++.h"
#define puba push_back
#define mapa make_pair
#define ff first
#define ss second
#define bend(_x) (_x).begin(), (_x).end()
#define szof(_x) ((int) (_x).size())
#define TASK_NAME "sequence"
#define M_PI 3.1415926

using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int MAXN = 3e5 + 5;

//a;
//b;
//c = b - a;
//d = c - b = -a;
//e = d - c = -a - b + a = -b;
//f = e - d = a - b;
//g = f - e = a - b + b = a;
//h = g - f = a - a + b = b;

int n;
int arr[MAXN];
vector <int> zip[6];
vector <ll> pref[6];
vector <ll> suff[6];

double check(double a, double b) {
    vector <double> v;
    v.puba(a);
    v.puba(b);

    for (int i = 2; i < 6; ++i) {
        double now = b - a;        
        v.puba(now);
        a = b;
        b = now;
    }

    double ret = 0;
    for (int i = 0; i < 6; ++i) {
        int num = upper_bound(bend(zip[i]), (int) v[i]) - zip[i].begin();
        while (num < szof(zip[i]) && zip[i][num] < v[i]) {
            ++num;
        }
        while (num > 0 && zip[i][num - 1] > v[i]) {
            --num;
        }
                
        if (num > 0) {
            ret += (ll) zip[i][num - 1] * num - pref[i][num];
            ret += (v[i] - zip[i][num - 1]) * num;
        }
        if (num < szof(zip[i])) {
            ret += suff[i][num] - v[i] * (szof(zip[i]) - num);
        }
    }
    return ret;
}

ll check2(long long a, long long b) {
    vector <ll> v;
    v.puba(a);
    v.puba(b);

    for (int i = 2; i < 6; ++i) {
        ll now = b - a;        
        v.puba(now);
        a = b;
        b = now;
    }

    ll ret = 0;
    for (int i = 0; i < 6; ++i) {
        int num = upper_bound(bend(zip[i]), (int) v[i]) - zip[i].begin();
        while (num < szof(zip[i]) && zip[i][num] < v[i]) {
            ++num;
        }
        while (num > 0 && zip[i][num - 1] > v[i]) {
            --num;
        }
                
        if (num > 0) {
            ret += (ll) zip[i][num - 1] * num - pref[i][num];
            ret += (ll) (v[i] - zip[i][num - 1]) * num;
        }
        if (num < szof(zip[i])) {
            ret += suff[i][num] - (ll) v[i] * (szof(zip[i]) - num);
        }
    }
    return ret;
}

int main() {        
    freopen(TASK_NAME ".in", "r", stdin);
    freopen(TASK_NAME ".out", "w", stdout);

    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", arr + i);
        zip[i % 6].puba(arr[i]);
    }

    for (int i = 0; i < 6; ++i) {
        sort(bend(zip[i]));
        pref[i].puba(0);
        suff[i].puba(0);
        for (int j = 0; j < szof(zip[i]); ++j) {
            pref[i].puba(pref[i].back() + zip[i][j]);
            suff[i].puba(suff[i].back() + zip[i][szof(zip[i]) - j - 1]);
        }
        reverse(bend(suff[i]));
    }

    double a = 0;
    double b = 0;
    double step = 1e13;
    double d = 0.5;
    vector <pair<double, double>> dxy;
    int ang = 8;
    for (int i = 0; i < ang; ++i) {
        dxy.puba(mapa(sin(M_PI * i / ang * 2), cos(M_PI * i / ang * 2)));
    }     

    int tc = 0;
    while (step > 0.5) {
        //cerr << "new" << endl;
        if (tc - 100 == 0) {
            tc = 0;
            if (clock() > CLOCKS_PER_SEC * 0.6) {
                break;
            }
        }
        int mem = -1;
        double best = check(a, b);
        //cerr << a << " " << b << " " << best << endl;
        ++tc;
        for (int i = 0; i < szof(dxy); ++i) {
            double nx = a + step * dxy[i].ff;
            double ny = b + step * dxy[i].ss;
            double tmp = check(nx, ny);
            //cerr << tmp << endl;
            //assert(tmp >= 0);
            if (tmp < best) {
                best = tmp;
                mem = i;
            }
        }

        if (mem != -1) {
            //cerr << "@" << endl;
            a = a + step * dxy[mem].ff;
            b = b + step * dxy[mem].ss;            
        } else {
            step *= d;
        }
        //cerr << step << endl;        
    }

    cerr << step << endl;

    //cerr << a << " " << b << endl;

    int ra = round(a);
    int rb = round(b);
    ll best = check2(ra, rb);
    int ansa = ra, ansb = rb;

    int delta = 2;
    
    for (int i = -delta; i <= delta; ++i) {
        for (int j = -delta; j <= delta; ++j) {
            ll tmp = check2((ll) a + i, (ll) b + j);
            if (tmp < best) {
                best = tmp;
                ansa = a + i;
                ansb = b + j;
            }
        }
    }

    //assert(best == check2(ansa, ansb));

    cout << best << "\n";
    /* << ansa << " " << ansb << " ";

    for (int i = 2; i < n; ++i) {
        int now = ansb - ansa;
        //printf("%d ", now);
        cout << now << " ";
        ansa = ansb;
        ansb = now;
    }
    //printf("\n");
    cout << "\n";*/

    return 0;
}