#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;
const LL INF = 1e18;

int main(){
	int N, M;
	cin >> N >> M;
	vector<LL> H(N), W(M);
	rep(i,N) cin >> H[i];
	rep(i,M) cin >> W[i];
	sort(H.begin(),H.end());
	vector<int> sum_left((N+1)/2), sum_right((N+1)/2);
	rep(i,(N-1)/2){
		sum_left[i+1] = sum_left[i] + (H[2*i+1] - H[2*i]);
		sum_right[i+1] = sum_right[i] + (H[2*i+2] - H[2*i+1]);
	}
	LL ans = INF;
	rep(i,M){
		int num = lower_bound(H.begin(),H.end(),W[i])-H.begin();
		LL right = sum_right[(N-1)/2] - sum_right[num/2];
		LL res = sum_left[num/2] + right;
		if(num % 2 == 0) res += H[num] - W[i];
		else res += W[i] - H[num-1];
		ans = min(ans, res);
	}
	cout << ans << endl;

	return 0;
}
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include<bits/stdc++.h>
#define itn int
#define ll long long
#define pir pair<ll,ll>
#define yes {puts("Yes");}
#define no {puts("No");}
using namespace std;

ll read(){
	ll a=0,b=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')b=-1;c=getchar();}
	while(c>='0'&&c<='9')a=a*10+c-48,c=getchar();
	return a*b;
}

int n,m;
struct edge{
	int v,nx;
	ll c,d;
}e[200005];
int cnt,hd[100005];

ll dis[100005];
bool vis[100005];

struct node{
	int i;
	ll w;
};
bool operator<(node a,node b){
	return a.w>b.w;
}

ll clac(ll t,ll c,ll d){
	if(!d)return c;
	ll sq=sqrt(d+2),ans=c+d/(t+1);
	for(ll st=t;st<=t+3;st++){
		ans=min(ans,d/(st+1)+st-t+c);
	}
	if(t+1<sq){
		for(ll st=t;st<=t+30;st++){
			ans=min(ans,d/(st+1)+st-t+c);
		}
		for(ll st=max(t,sq-30);st<=sq+5;st++){
			ans=min(ans,d/(st+1)+st-t+c);
		}
	}
	return ans;
}

void dij(){
	memset(dis,0x3f,sizeof dis);
	dis[1]=0;
	priority_queue<node>q;
	q.push(node{1,0});
	while(!q.empty()){
		node t=q.top();q.pop();
		if(vis[t.i])continue;
		vis[t.i]=1;
		for(int i=hd[t.i];i;i=e[i].nx){
			int v=e[i].v;
			ll w=clac(dis[t.i],e[i].c,e[i].d);
			if(dis[v]>dis[t.i]+w){
				dis[v]=dis[t.i]+w;
				q.push(node{v,dis[v]});
			}
		}
	}
}

int main(){
	n=read(),m=read();
	while(m--){
		int a=read(),b=read(),c=read(),d=read();
		e[++cnt]=edge{b,hd[a],c,d},hd[a]=cnt;
		e[++cnt]=edge{a,hd[b],c,d},hd[b]=cnt;
	}
	dij();
	if(dis[n]>1e18)cout<<-1;
	else cout<<dis[n]<<'\n';
	return 0;
}

