#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
int min(int o, int p){
if (o<p) return o; else return p;
}
int main(){
freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);
int n,x;
int a[105],b[105];
cin>>n;
for (int i=1; i<=n;++i)
cin>>a[i];
for (int i=1; i<=n;++i)
for (int j=i+1; j<=n;++j)
if (a[i]>a[j]){
x=a[i]; a[i]=a[j]; a[j]=x;
}
a[0]=30000; b[-1]=0;
a[-1]=0; b[0]=0;
for (int i=1; i<=n; ++i)
b[i]=min(b[i-1],b[i-2])+abs(a[i]-a[i-1]);
cout<<b[n]<<endl;
fclose (stdin);
fclose (stdout);
}