#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  vector<int> index(N);
  for (int i = 0; i < N; i++) index.push_back(i);
  int mi = 1e9;
  do {
    int cur = 0;
    for (int i = 0; i < N - 1; i++) {
      cur += abs(A[i] - A[i + 1]);
    }
    if (mi > cur) mi = cur;
  } while (next_permutation(A.begin(), A.end()));
  cout << mi << endl;
  return 0;
}
