#include<bits/stdc++.h>
#define For(a,b,c) for(int a=b;a<=c;++a)
#define Dor(a,b,c) for(int a=b;a>=c;--a)
using namespace std;
enum{N=207};
int n,X[N],Y[N],R[N],A[N],B[N],C[N],D[N],P[N];
int O(int x,int y,int z) {
	return x<=y&&y<=z;
}
int banana(int a,int b) {
	return (
	(O(A[a],A[b],C[a])||O(A[a],C[b],C[a])||O(A[b],A[a],C[b]))&&
	(O(B[a],B[b],D[a])||O(B[a],D[b],D[a])||O(B[b],B[a],D[b])));
}
bool cmp(int x,int y) {
	return R[x]<R[y];
}
int main() {
	scanf("%d",&n);
	For(i,1,n) scanf("%d%d%d",X+i,Y+i,R+i),P[i]=i,A[i]=X[i],B[i]=Y[i],C[i]=X[i]+1,D[i]=Y[i]+1;
	sort(P+1,P+1+n,cmp);
	For(i,1,n) {
		int u=P[i],l=1,r=min(int(sqrt(R[u])/2),min(10000-X[u],10000-Y[u]));
		while(l<=r) {
			int m=(l+r)>>1,o=1;
			C[u]=A[u]+m,D[u]=B[u]+m;
			For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;}
			if(o) l=m+1;
			else r=m-1;
		}
		C[u]=A[u]+r,D[u]=B[u]+r;
		l=1,r=min(int(sqrt(R[u])/2),min(X[u],Y[u]));
		while(l<=r) {
			int m=(l+r)>>1,o=1;
			A[u]=X[u]+1-m,B[u]=Y[u]+1-m;
			For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;}
			if(o) l=m+1;
			else r=m-1;
		}
		A[u]=X[u]+1-r,B[u]=Y[u]+1-r;
		
		l=1,r=min(R[u]/(C[u]-A[u])+Y[u],10000)-Y[u];
		while(l<=r) {
			int m=(l+r)>>1,o=1;
			D[u]=Y[u]+m;
			For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;}
			if(o) l=m+1;
			else r=m-1;
		}
		D[u]=Y[u]+r;
		
		l=1,r=Y[u]-max(D[u]-R[u]/(C[u]-A[u]),1);
		while(l<=r) {
			int m=(l+r)>>1,o=1;
			B[u]=Y[u]-m;
			For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;}
			if(o) l=m+1;
			else r=m-1;
		}
		B[u]=Y[u]-r;
	}
	For(i,1,n) {
		int u=P[i],l,r;
		l=1,r=min(R[u]/(D[u]-B[u])+X[u],10000)-X[u];
		while(l<=r) {
			int m=(l+r)>>1,o=1;
			C[u]=X[u]+m;
			For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;}
			if(o) l=m+1;
			else r=m-1;
		}
		C[u]=X[u]+r;
		
		l=1,r=X[u]-max(C[u]-R[u]/(D[u]-B[u]),1);
		while(l<=r) {
			int m=(l+r)>>1,o=1;
			A[u]=X[u]-m;
			For(j,1,n) if(u!=j&&banana(u,j)) {o=0; break;}
			if(o) l=m+1;
			else r=m-1;
		}
		A[u]=X[u]-r;
	}
	For(i,1,n) printf("%d %d %d %d\n",A[i],B[i],C[i],D[i]);
	return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
//#include <queue>
//#include <stack>
#include <cstring>
#include <cassert>

using namespace std;

#ifdef LOCAL_DEBUG
#include <local_debug.h>
#define DEBUG(...) DBG2::print(#__VA_ARGS__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<llong> VL;
typedef vector<VL> VVL;

const llong INF = 3e18;

int N, M;
VI H, W;
int nearest_teacher_height(int h, bool trace = false) {
   int idx = lower_bound(W.begin(), W.end(), h) - W.begin();
   if (trace)
      DEBUG(idx);
   if (idx == SZ(W)) {
      --idx;
      assert(idx >= 0);
      if (trace)
         DEBUG(1);
      return W[idx];
   }
   if (idx == 0) {
      if (trace)
         DEBUG(2);
      return W[idx];
   }
   if (trace)
      DEBUG(3, W[idx-1], W[idx]);
   if (abs(W[idx-1] - h) < abs(W[idx] - h))
      return W[idx-1];
   else
      return W[idx];
}


llong memo[2][200002];
llong go(int n, bool t) {
   if (n >= SZ(H))
      return 0;
   llong& res = memo[t][n];
   if (res < 0) {
      bool trace = n == 4 && !t;
      // match n-th student with next student
      res = INF;
      if (n+1 < N) {
         llong cur = go(n+2, t) + llabs(H[n+1] - H[n]);
         if (trace)
            DEBUG("1", cur);
         res = min(res, cur);
      }
      
      // match n-th student to nearest height of teacher
      if (!t) {
         int w = nearest_teacher_height(H[n]);
         if (trace)
            DEBUG(H[n], w);
         llong cur = go(n+1, true) + llabs(H[n] - w);
         if (trace)
            DEBUG("2", cur);
         res = min(res, cur);
      }
   }
   return res;
}

int main(int argc, char* argv[]) {
   ios_base::sync_with_stdio(false); 
   cin.tie(nullptr);

   cin >> N >> M;
   H = VI(N);
   REP(i, N)
      cin >> H[i];

   W = VI(M);
   REP(j, M)
      cin >> W[j];

   sort(H.begin(), H.end());
   sort(W.begin(), W.end());
   
   DEBUG(H);
   DEBUG(W);
   
// int t = nearest_teacher_height(7, true);
// DEBUG(t);
   
   memset(memo, -1, sizeof(memo));
   llong res = go(0, 0);
   cout << res << endl;

   return 0;
}
