#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
using namespace std;
const int maxn = 5e5 + 10;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n, a[maxn];
int p[maxn];
long long best;
void check()
{
for (int i = 1; i <= n; ++ i)
{
int curr = p[i];
int onleft = 0, onright = 0;
for (int j = i-1; j >= 1; -- j)
{
if(curr == p[j])continue;
if(curr < p[j])onleft = 1;
if(curr > p[j])onleft = -1;
if(onleft)break;
}
for (int j = i+1; j <= n; ++ j)
{
if(curr == p[j])continue;
if(curr < p[j])onright = 1;
if(curr > p[j])onright = -1;
if(onright)break;
}
if(onright && onleft && onleft != onright)return;
}
long long ans = 0;
for (int i = 1; i <= n; ++ i)
ans += abs(a[i] - p[i]);
best = min(best, ans);
}
void gen(int pos)
{
if(pos > n)
{
check();
return;
}
for (int i = 0; i <= 20; ++ i)
{
p[pos] = i;
gen(pos+1);
}
}
int main()
{
speed();
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
best = 1e9 + 10;
gen(1);
cout << best << endl;
return 0;
}