#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64_t)1e18
#define INF (int32_t)1e9

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  
  for (auto &&e : a) {
    cin >> e;
  }
  vector<int> ord(N);
  iota(ord.begin(), ord.end(), 0);
  int res = INF;
  
  do {
    int sum = 0;
    
    for (int i = 0; i < N-1; i++) {
      int dist = abs(a[ord[i]] - a[ord[i+1]]);
      sum += dist;
    }
    
    res = min(res, sum);
  } while(next_permutation(ord.begin(), ord.end()));

  cout << res << endl;
}
