#include <bits/stdc++.h>

using namespace std;

vector <int> x, y, z;
vector <long long> sum_x, sum_y, sum_z;

void build_prefix_sums(vector <int> &a, vector <long long> &sum) {
  sort(a.begin(), a.end());
  sum.resize(a.size() + 1);
  sum[0] = 0;
  for (int i = 0; i < (int)a.size(); i++) {
    sum[i + 1] = sum[i] + a[i];
  }
}

inline long long get_sum(vector <int> &a, vector <long long> &sum, int v, int pos) {
  int cnt = a.size();
  if (pos == -1) {
    pos = lower_bound(a.begin(), a.end(), v) - a.begin();
  }
  return (v * 1LL * pos - sum[pos]) + ((sum[cnt] - sum[pos]) - v * 1LL * (cnt - pos));
}

const long long inf = (long long)1e18;

long long ans = inf;

long long test_answer(int rot, int p1, int p2) {
  int xx = 0, yy = 0, zz = 0;
  if (rot == 0) xx = x[p1], yy = y[p2], zz = y[p2] - x[p1];
  if (rot == 1) xx = y[p1] - z[p2], yy = y[p1], zz = z[p2];
  if (rot == 2) xx = x[p2], yy = x[p2] + z[p1], zz = z[p1];
  long long cur = get_sum(x, sum_x, xx, (rot == 0 ? p1 : (rot == 2 ? p2 : -1)))
                + get_sum(y, sum_y, yy, (rot == 0 ? p2 : (rot == 1 ? p1 : -1)))
                + get_sum(z, sum_z, zz, (rot == 1 ? p2 : (rot == 2 ? p1 : -1)));
  if (cur < ans) {
    ans = cur;
  }
  return cur;
}

void get(int &x) {
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  bool flag = false;
  if (ch == '-') {
    flag = true;
    ch = getchar();
  }
  x = 0;
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (flag) x = -x;
}

int main() {
  freopen("sequence.in", "r", stdin);
  freopen("sequence.out", "w", stdout);
  int n;
  get(n);
  vector <int> a(n);
  for (int i = 0; i < n; i++) {
    get(a[i]);
  }
  for (int i = 0; i < n; i++) {
    vector <int> &to = (i % 3 == 0 ? x : (i % 3 == 1 ? y : z));
    to.push_back(i / 3 % 2 == 0 ? a[i] : -a[i]);
  }
  build_prefix_sums(x, sum_x);
  build_prefix_sums(y, sum_y);
  build_prefix_sums(z, sum_z);
  for (int rot = 0; rot < 3; rot++) {
    vector <int> p = (rot == 0 ? x : (rot == 1 ? y : z));
    vector <int> q = (rot == 0 ? y : (rot == 1 ? z : x));
    if (rot == 2) {
      reverse(q.begin(), q.end());
    }
    int pn = p.size();
    int qn = q.size();
    int j = 0;
    for (int i = 0; i < pn; i++) {
      test_answer(rot, i, (rot == 2 ? qn - j - 1 : j));
      while (j + 1 < qn && test_answer(rot, i, (rot == 2 ? qn - j - 1 : j)) >=
                           test_answer(rot, i, (rot == 2 ? qn - (j + 1) - 1 : j + 1))) {
        j++;
      }
    }
    cerr << "unique: " << (unique(q.begin(), q.end()) - q.begin()) << endl;
  }
  cout << ans << endl;
  return 0;
}
