#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, q, a, b, l, val[800007][3], tag[800007];
int read()
{
	int num = 0;
	char c = getchar();
	while (c<'0' || c>'9')c = getchar();
	while (c >= '0' && c <= '9')num = num * 10 + c - '0', c = getchar();
	return num;
}
int add(int w, int v)
{
	for (int j = 0; j <= 2; j++)
		val[w][j] += v;
	tag[w] += v;
	return 0;
}
int down(int w)
{
	if (!tag[w])return 0;
	add(2 * w, tag[w]);
	add(2 * w + 1, tag[w]);
	tag[w] = 0;
	return 0;
}
int up(int w)
{
	for (int i = 0; i <= 2; i++)
		val[w][i] = min(val[2 * w][i], val[2 * w + 1][i]);
	return 0;
}
int change(int w, int l, int r, int x, int v)
{
	if (l == r)
	{
		val[w][0] = v;
		val[w][1] = v - x;
		val[w][2] = v + x;
		return 0;
	}
	down(w);
	int mid = (l + r) / 2;
	if (x <= mid)change(2 * w, l, mid, x, v);
	else change(2 * w + 1, mid + 1, r, x, v);
	up(w);
	return 0;
}
int ask(int p, int w, int l, int r, int x, int y)
{
	if (x <= l && r <= y)
		return val[w][p];
	down(w);
	int mid = (l + r) / 2, res = 1e18;
	if (x <= mid)res = min(res, ask(p, 2 * w, l, mid, x, y));
	if (y > mid)res = min(res, ask(p, 2 * w + 1, mid + 1, r, x, y));
	return res;
}
signed main()
{
	n = read();
	q = read();
	a = read();
	b = read();
	l = read();
	memset(val, 0x3f, sizeof val);
	change(1, 1, n, b, abs(l - a));
	change(1, 1, n, a, abs(l - b));
	for (int i = 2; i <= q; i++)
	{
		int num = read(), len = abs(num - l);
		int res1 = ask(0, 1, 1, n, num, num) + len;
		int res2 = ask(1, 1, 1, n, 1, num) + num;
		int res3 = ask(2, 1, 1, n, num + 1, n) - num;
		add(1, len);
		change(1, 1, n, l, min(res1, min(res2, res3)));
		l = num;
	}
	printf("%lld\n", val[1][0]);
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[32;1m" << #x << "->" ; err(x);} while(0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T,typename t,typename... A>
void err(T<t> a,A... x){for (auto v:a) cout << v << ' '; err(x...);}
template<typename T,typename... A>
void err(T a,A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
const int mod=998244353;
const int INF=0x3f3f3f3f;
ll quick(ll a,ll b)
{
	ll ret=1;
	while(b)
	{
		if(b&1)
			ret=ret*a%mod;
		a=a*a%mod;
	}
	return ret;
}
ll inv(ll a){return quick(a,mod-2);}
const int maxn=1e5+7;
ll l[maxn],u[maxn],b[maxn];
struct val{
	ll value;
	int id;
	bool operator<(const val &a)const{
		return value>a.value;
	}
}v[maxn];
ll n,x;
ll tot;
bool check(ll cur)
{
	ll cnt=cur/x;
	ll ret=0;
	ll rm=cur%x;
	for(int i=0;i<cnt+(rm!=0);i++)
		ret+=v[i].value;
	ll mx=0;
	if(rm)
	{
		for(int i=0;i<n;i++)
		{
			int id=v[i].id;
			mx=max(mx,ret-(i>=cnt?v[cnt].value:v[i].value)+(rm>b[id]?(rm-b[id])*u[id]+b[id]*l[id]:rm*l[id]));
		}
	}
	else mx=ret;
	return mx>=tot;
}
int main()
{
	scanf("%lld%lld",&n,&x);
	tot=0;
	for(int i=0;i<n;i++)
	{
		scanf("%lld%lld%lld",&b[i],&l[i],&u[i]);
		tot+=l[i]*b[i];
		v[i].value=b[i]*l[i]+(x-b[i])*u[i];
		v[i].id=i;
		//dbg(i,v[i].value);
	}
	sort(v,v+n);
	ll L=0,R=n*x;
	ll ans;
	while(L<=R)
	{
		ll mid=L+R>>1;
		if(check(mid)) R=mid-1,ans=mid;
		else L=mid+1;
	}
	printf("%lld\n",ans);
}
