#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
struct DP3
{
	int N1, N2, N3;
	vector<T> data;
	DP3(int N1, int N2, int N3, const T& t = T())
		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2, int i3)
		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3& rhs)
		{ data.swap(rhs.data); }
};

int bitcnt(int U)
{
	int c = 0;
	for(; U; U>>=1)
		c += (U&1);
	return c;
}

class TicketPrinters { public:
	int minTime(int currentPrinter, vector <int> printerDistance, vector <int> startingValues, vector <int> wantedValues) 
	{
		int N = startingValues.size();

		vector<int> d(N);
		partial_sum( printerDistance.begin(), printerDistance.end(), d.begin()+1 );

		// left, used, LeftOrRight
		DP3<int> dp(N,1<<N,2);
		for(int D=(1<<N)-1; D>=0; --D)
		{
			int bc = bitcnt(D);
			for(int L=0; L<N && L+bc<=N; ++L)
				for(int C=0; C<=1; ++C)
				{
					if( bc == N )
						dp(L,D,C) = 0;
					else {
						int cur = (C==0 ? L : L+bc-1);
						int mn = 0x7fffffff;
						for(int nextW=0; nextW<N; ++nextW)
							if( ((1<<nextW) & D) == 0 ) {
								int caseLeft = 0x7fffffff;
								if( L>0 ) {
									caseLeft = d[cur] - d[L-1] + max(dp(L-1, D|1<<nextW, 0),
										1+abs(startingValues[L-1]-wantedValues[nextW]));
								}
								int caseRight = 0x7fffffff;
								if( L+bc < N ) {
									caseRight = d[L+bc] - d[cur] + max(dp(L, D|1<<nextW, 1),
										1+abs(startingValues[L+bc]-wantedValues[nextW]));
								}
								mn = min(mn, min(caseLeft, caseRight));
							}
						dp(L,D,C) = mn;
					}
				}
		}
		return dp(currentPrinter,0,0);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, TicketPrinters().minTime(currentPrinter, printerDistance, startingValues, wantedValues));}
int main(){

CASE(0)
	int currentPrinter = 0; 
	int printerDistance_[] = {100, 20, 50};
	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_)); 
	int startingValues_[] = {66, 78, 99, 5};
	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_)); 
	int wantedValues_[] = {99, 5, 78, 66};
	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_)); 
	int _ = 171; 
END
CASE(1)
	int currentPrinter = 1; 
	int printerDistance_[] = {50, 50};
	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_)); 
	int startingValues_[] = {100, 200, 300};
	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_)); 
	int wantedValues_[] = {101, 201, 302};
	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_)); 
	int _ = 152; 
END
CASE(2)
	int currentPrinter = 2; 
	int printerDistance_[] = {13, 26, 39, 13};
	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_)); 
	int startingValues_[] = {123, 12, 32, 67, 1015};
	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_)); 
	int wantedValues_[] = {1, 2, 3, 4, 5};
	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_)); 
	int _ = 1063; 
END
	/*
CASE(3)
	int currentPrinter = ; 
	int printerDistance_[] = ;
	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_)); 
	int startingValues_[] = ;
	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_)); 
	int wantedValues_[] = ;
	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_)); 
	int _ = ; 
END
CASE(4)
	int currentPrinter = ; 
	int printerDistance_[] = ;
	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_)); 
	int startingValues_[] = ;
	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_)); 
	int wantedValues_[] = ;
	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
