#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <vector>
#include <random>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>

//#include <atcoder/all>

#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=998244353;
const int dx[8]={-1,0,1,0,1,1,-1,-1},dy[8]={0,1,0,-1,1,-1,1,-1};

//using namespace atcoder;

int n,m;
vi a;

vi f(){
	vi b(n/2+1);
	for(int i=1;i<=n/2;i++){
		b[i]=abs(a[2*i-1]-a[2*i-2])+b[i-1];
	}
	reverse(a.begin(),a.end());
	return b;
}

int main(){
	cin>>n>>m;
	a=vi(n);
	for(auto &i:a) cin>>i;
	sort(a.begin(),a.end());
	vi b=f(),c=f();
	int res=inf;
	for(int i=0;i<m;i++){
		int x;
		cin>>x;
		int j=upper_bound(a.begin(),a.end(),x)-a.begin();
		res=min(res,b[j/2]+c[(n-j)/2]+abs(x-a[(j%2==1?j-1:j)]));
	}
	cout<<res<<endl;
}











//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include <string.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define INF 2e9
using namespace std;
typedef long long ll;//int64
typedef unsigned long long ull;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
template<class T> void chmax(T& a, T b){if(a<b)a=b;}
template<class T> void chmin(T& a, T b){if(a>b)a=b;}

ll n,x[20],y[20],z[20];
ll dp[1<<17][20];

ll dif(int i1,int i2){
    return abs(x[i1]-x[i2])+abs(y[i1]-y[i2])+max(0LL,z[i2]-z[i1]);
}

int main(){
    cin >> n;
    REP(i,n)cin >> x[i] >> y[i] >> z[i];

    REP(i,n)REP(j,1<<n)dp[j][i] = 1LL<<60;
    dp[1][0] = 0;
    REP(S,1<<n){
        REP(x,n){
            REP(y,n){
                if(S & (1<<x)){
                    dp[S|(1<<y)][y] = min(dp[S|(1<<y)][y], dp[S][x]+dif(x,y));
                }
            }
        }

    }    

    cout << dp[(1<<n)-1][0] << endl;   
    
    return 0;
}