#include<set>
#include<ctime>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e4+7;
int read(){
	char c;
	int x=0,f=1;
	while(!isdigit(c=getchar()))
		f-=2*(c=='-');
	while(isdigit(c)){
		x=x*10+f*(c-48);
		c=getchar();
	}
	return x;
}
struct que{
	int p,i;
	que(int a=0,int b=0){
		p=a;
		i=b;
	}
	friend bool operator <(que a,que b){
		return a.p<b.p;
	}
};
struct sol{
	int a,b,c,d;
	sol(int A=0,int B=0,int C=0,int D=0){
		a=A;
		b=B;
		c=C;
		d=D;
	}
};
int c[N];
bool vis[N];
int n,x,y,r,tot;
vector<que>l[N];
sol ans[N];
int random(int l,int r){
	return rand()%(r-l+1)+l;
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("A.in","r",stdin);
	freopen("A.out","w",stdout);
	#endif
	clock_t t1=clock();
	//--------
	n=read();
	srand(210280344);
	for(int i=1;i<=n;++i){
		x=read();
		y=read();
		r=read();
		l[x].push_back(que(y,i));
		vis[x]=1;
	}
	for(int i=0;i<N-7;++i){
		if(vis[i]){
			++tot;
			c[tot]=i;
			sort(l[i].begin(),l[i].end());
		}
	}
	c[tot+1]=N-7;
	for(int i=1;i<=tot;++i){
		int A;
		int B;
		int C;
		int D;
		int sz=l[c[i]].size();
		if(i==1)
			A=random(0,c[i]);
		else
			A=random(c[i-1],c[i]);
		C=random(c[i]+1,c[i+1]);
		B=random(0,l[c[i]][0].p);
		if(sz==1){
			D=random(l[c[i]][0].p+1,N-7);
			ans[l[c[i]][0].i]=sol(A,B,C,D);
			c[i]=C;
			continue;
		}
		D=random(l[c[i]][0].p+1,l[c[i]][1].p);
		l[c[i]][0].p=D;
		ans[l[c[i]][0].i]=sol(A,B,C,D);
		for(int j=1;j<sz-1;++j){
			B=random(l[c[i]][j-1].p,l[c[i]][j].p);
			D=random(l[c[i]][j].p+1,l[c[i]][j+1].p);
			l[c[i]][j].p=D;
			ans[l[c[i]][j].i]=sol(A,B,C,D);
		}
		B=random(l[c[i]][sz-2].p,l[c[i]][sz-1].p);
		D=random(l[c[i]][sz-1].p+1,N-7);
		ans[l[c[i]][sz-1].i]=sol(A,B,C,D);
		c[i]=C;
	}
	for(int i=1;i<=n;++i){
		int A=ans[i].a;
		int B=ans[i].b;
		int C=ans[i].c;
		int D=ans[i].d;
		printf("%d %d %d %d\n",A,B,C,D);
	}
	//--------
	fprintf(stderr,"%0.3lfs",1.0*(clock()-t1)/CLOCKS_PER_SEC);
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

void chmin(long long int& a, long long int b)
{
    if (a > b) {
        a = b;
    }
}

int main()
{
    int N, M;
    cin >> N >> M;

    // 入力する
    vector<long long int> H(N);
    vector<long long int> W(M);
    for (int i = 0; i < N; i++) {
        cin >> H[i];
    }
    for (int i = 0; i < M; i++) {
        cin >> W[i];
    }

    // vectorをソートする
    sort(H.begin(), H.end());

    vector<long long int> sum1((N + 1) / 2);
    vector<long long int> sum2((N + 1) / 2);
    for (int i = 0; i + 1 < N; i += 2) {
        sum1[i / 2 + 1] = sum1[i / 2] + H[i + 1] - H[i];
    }
    for (int i = N - 2; i > 0; i -= 2) {
        sum2[i / 2] = sum2[i / 2 + 1] + H[i + 1] - H[i];
    }

    long long int res = LLONG_MAX;

    // lower_boundで各W_iの入るindexを調べる
    for (long long int w : W) {
        long long int index = lower_bound(H.begin(), H.end(), w) - H.begin();
        if (index & 1) {
            index ^= 1;
        }
        chmin(res, sum1[index / 2] + sum2[index / 2] + abs(H[index] - w));
    }

    cout << res << endl;
}
