#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;

void solve(int n){
	int y,j,i,m,t,t1,x,e,q,w;
	long long int a=0,b=0,s,d,ans=0,f,dd=0,a1=0,b1=0;
	scanf("%I64d",&s);
	vector<pair<long long int,long long int>> aa,bb;
	pair<long long int,long long int> p;
	for(i=0;i<n;i++){
		scanf("%d%d%d",&q,&w,&e);
		if(w==e){
			dd+=q;
			d=w;
			ans+=d*q;
		} else {
			d=max(w,e);
			ans+=d*q;
			d=min(w,e);
			if(d==e){
				p.first=w-d;
				p.second=q;
				aa.push_back(p);
				a+=q;
			} else {
				p.first=e-d;
				p.second=q;
				bb.push_back(p);
				b+=q;
			}
		}
	}
	if((a+s-1)/s+(b+s-1)/s<=(a+b+dd+s-1)/s){
		printf("%I64d",ans);
		return;
	}

	sort(aa.begin(),aa.end());
	sort(bb.begin(),bb.end());

	a%=s;
	for(i=0;a>0 && i<aa.size();i++){
		a1+=aa[i].first*min(a,aa[i].second);
		a-=min(a,aa[i].second);
	}

	b%=s;
	for(i=0;b>0 && i<bb.size();i++){
		b1+=bb[i].first*min(b,bb[i].second);
		b-=min(b,bb[i].second);
	}

	printf("%I64d",ans-min(a1,b1));
}

int main() {
#pragma warning(disable:4996);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
#ifndef _DEBUG
	//	freopen("changestr.in", "r", stdin);
	//	freopen("changestr.out", "w", stdout);
#endif
	int a,b,n;
	while(scanf("%d",&n)!=EOF){
		solve(n);
		printf("\n");
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n, m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		if (y > z)
		{
			arr1.push_back(make_pair(y - z, x));
			slices1 += x;
			cnt += y*x;
		}
		else {
			arr2.push_back(make_pair(z - y, x));
			slices2 += x;
			cnt += z*x;
		}
	}
	int temp1 = slices1 % m;
	int temp2 = slices2 % m;
	if (temp1 + temp2 > m)
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end());
	sort(arr2.begin(), arr2.end());
	long long cnt1 = 0, cnt2 = 0;
	int i = 0;
	while (temp1 > 0)
	{
		cnt1 += min(temp1, arr1[i].second) * arr1[i].first;
		temp1 -= min(temp1, arr1[i].second);
		i++;
	}
	i = 0;
	while (temp2 > 0)
	{
		cnt2 += min(temp2, arr2[i].second) * arr2[i].first;
		temp2 -= min(temp2, arr2[i].second);
		i++;
	}
	cout << cnt - min(cnt1, cnt2);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n, m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices = 0, slices1 = 0, slices2 = 0, similar = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else{
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
	}
	int temp1 = slices1 % m;
	int temp2 = slices2 % m;
	if (temp1 + temp2 > m)
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	int cnt1 = 0, cnt2 = 0;
	int i = 0;
	while (temp1 > 0)
	{
		cnt1 += min(temp1, arr3[arr1[i].second]) * diff[arr1[i].second];
		temp1 -= min(temp1, arr3[arr1[i].second]);
		i++;
	}
	i = 0;
	while (temp1 > 0)
	{
		cnt1 += min(temp1, arr3[arr1[i].second]) * diff[arr1[i].second];
		temp1 -= min(temp1, arr3[arr1[i].second]);
		i++;
	}
	cout << cnt - min(cnt1, cnt2);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n, m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices = 0, slices1 = 0, slices2 = 0, similar = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else{
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
	}
	int temp1 = slices1 % m;
	int temp2 = slices2 % m;
	if (temp1 + temp2 > m)
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	int cnt1 = 0, cnt2 = 0;
	int i = 0;
	while (temp1 > 0)
	{
		cnt1 += min(temp1, arr3[arr1[i].second]) * diff[arr1[i].second];
		temp1 -= min(temp1, arr3[arr1[i].second]);
		i++;
	}
	i = 0;
	while (temp2 > 0)
	{
		cnt2 += min(temp2, arr3[arr2[i].second]) * diff[arr2[i].second];
		temp2 -= min(temp2, arr3[arr2[i].second]);
		i++;
	}
	cout << cnt - min(cnt1, cnt2);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n; double m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else{
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
	}
	if (ceil(slices1 / m) + ceil(slices2 / m) == ceil(slices / m))
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	int cnt1 = 0, cnt2 = 0;
	int temp1 = slices1 - floor(slices1 / m) * m;
	int temp2 = slices2 - floor(slices2 / m) * m;
	for (int i = 0; i < n; i++)
	{
		if (temp1 > 0 && temp1 > arr3[arr1[i].second])
		{
			cnt1 += arr3[arr1[i].second] * diff[arr1[i].second];
			temp1 -= arr3[arr1[i].second];
		}
		else if (temp1 > 0 && temp1 <= arr3[arr1[i].second])
		{
			cnt1 += temp1*diff[arr1[i].second];
			temp1 = 0;
		}
		if (temp2 > 0 && temp2 > arr3[arr2[i].second])
		{
			cnt2 += arr3[arr2[i].second] * diff[arr2[i].second];
			temp2 -= arr3[arr2[i].second];
		}
		else if (temp2 > 0 && temp2 <= arr3[arr2[i].second])
		{
			cnt2 += temp2*diff[arr2[i].second];
			temp2 = 0;
		}
		if (temp1 == 0 && temp2 == 0)
			break;
	}
	if (cnt1 <= cnt2 && temp1 <= m- temp2)
		cout << cnt - cnt1;
	else if (cnt2 > cnt1 && temp2 <= m - temp1)
		cout << cnt - cnt2;
	else cout << cnt;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n; double m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt =0, slices = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else {
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	while (ceil(slices1 / m) + ceil(slices2 / m) > ceil(slices / m))
	{
		int cnt1 = 0, cnt2 = 0;
		int temp1 = (m == 1) ? 1 : slices1 - floor(slices1 / m) * m;
		int temp2 = (m == 1) ? 1 : slices2 - floor(slices2 / m) * m;
		for (int i = 0; i < n; i++)
		{
			if (temp1 > 0 && temp1 > arr3[arr1[i].second] )
			{
				cnt1 += arr3[arr1[i].second] * diff[arr1[i].second];
				temp1 -= arr3[arr1[i].second];
			}
			else if (temp1 > 0 && temp1 <= arr3[arr1[i].second])
			{
				cnt1 += temp1*diff[arr1[i].second];
				temp1 = 0;
			}
			if (temp2 > 0 && temp2 > arr3[arr2[i].second] )
			{
				cnt2 += arr3[arr2[i].second] * diff[arr2[i].second];
				temp2 -= arr3[arr2[i].second];
			}
			else if (temp2 > 0 && temp2 <= arr3[arr2[i].second])
			{
				cnt2 += temp2*diff[arr2[i].second];
				temp2 = 0;
			}
			if (temp1 == 0 && temp2 == 0)
				break;
		}
		if (cnt1 <= cnt2)
		{
			cnt -= cnt1;
			slices1 -= (m == 1) ? 1 : slices1 - floor(slices1 / m) * m;
		}
		else {
			cnt -= cnt2;
			slices2 -= (m == 1) ? 1 : slices2 - floor(slices2 / m) * m;
		}
	}
	cout << cnt;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n; double m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices = 0, slices1 = 0, slices2 = 0, similar = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else if (z > y)
		{
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
		else
		{
			similar += x;
			slices -= x;
			cnt += z*x;
		}
	}
	if (ceil(slices1 / m) + ceil(slices2 / m) == ceil(slices / m))
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	int cnt1 = 0, cnt2 = 0;
	int temp1 = slices1 - floor(slices1 / m) * m;
	int temp2 = slices2 - floor(slices2 / m) * m;
	for (int i = 0; i < n; i++)
	{
		if (temp1 > 0 && temp1 > arr3[arr1[i].second])
		{
			cnt1 += arr3[arr1[i].second] * diff[arr1[i].second];
			temp1 -= arr3[arr1[i].second];
		}
		else if (temp1 > 0 && temp1 <= arr3[arr1[i].second])
		{
			cnt1 += temp1*diff[arr1[i].second];
			temp1 = 0;
		}
		if (temp2 > 0 && temp2 > arr3[arr2[i].second])
		{
			cnt2 += arr3[arr2[i].second] * diff[arr2[i].second];
			temp2 -= arr3[arr2[i].second];
		}
		else if (temp2 > 0 && temp2 <= arr3[arr2[i].second])
		{
			cnt2 += temp2*diff[arr2[i].second];
			temp2 = 0;
		}
		if (temp1 == 0 && temp2 == 0)
			break;
	}
	temp1 = slices1 - floor(slices1 / m) * m;
	temp2 = slices2 - floor(slices2 / m) * m;
	if (cnt1 <= cnt2 && temp1 + similar <= m - temp2)
		cout << cnt - cnt1;
	else if (cnt2 < cnt1 && temp2 + similar <= m - temp1)
		cout << cnt - cnt2;
	else cout << cnt;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n; double m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else if(z>y){
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
		else {
			slices -= x;
			cnt += z*x;
		}
	}
	if (ceil(slices1 / m) + ceil(slices2 / m) == ceil(slices / m))
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	int cnt1 = 0, cnt2 = 0;
	int temp1 = slices1 - floor(slices1 / m) * m;
	int temp2 = slices2 - floor(slices2 / m) * m;
	for (int i = 0; i < n; i++)
	{
		if (temp1 > 0 && temp1 > arr3[arr1[i].second])
		{
			cnt1 += arr3[arr1[i].second] * diff[arr1[i].second];
			temp1 -= arr3[arr1[i].second];
		}
		else if (temp1 > 0 && temp1 <= arr3[arr1[i].second])
		{
			cnt1 += temp1*diff[arr1[i].second];
			temp1 = 0;
		}
		if (temp2 > 0 && temp2 > arr3[arr2[i].second])
		{
			cnt2 += arr3[arr2[i].second] * diff[arr2[i].second];
			temp2 -= arr3[arr2[i].second];
		}
		else if (temp2 > 0 && temp2 <= arr3[arr2[i].second])
		{
			cnt2 += temp2*diff[arr2[i].second];
			temp2 = 0;
		}
		if (temp1 == 0 && temp2 == 0)
			break;
	}
	if (cnt1 <= cnt2)
		cout << cnt - cnt1;
	else cout << cnt - cnt2;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n, m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		if (y > z)
		{
			arr1.push_back(make_pair(y - z, x));
			slices1 += x;
			cnt += y*x;
		}
		else {
			arr2.push_back(make_pair(z - y, x));
			slices2 += x;
			cnt += z*x;
		}
	}
	int temp1 = slices1 % m;
	int temp2 = slices2 % m;
	if (temp1 + temp2 > m)
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end());
	sort(arr2.begin(), arr2.end());
	long long cnt1 = 0, cnt2 = 0;
	int i = 0;
	while (temp1 > 0)
	{
		cnt1 += min(temp1, arr1[i].second) * (long long)arr1[i].first;
		temp1 -= min(temp1, arr1[i].second);
		i++;
	}
	i = 0;
	while (temp2 > 0)
	{
		cnt2 += min(temp2, arr2[i].second) *  (long long)arr2[i].first;
		temp2 -= min(temp2, arr2[i].second);
		i++;
	}
	cout << cnt - min(cnt1, cnt2);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n, m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		if (y > z)
		{
			arr1.push_back(make_pair(y - z, x));
			slices1 += x;
			cnt += y*x;
		}
		else {
			arr2.push_back(make_pair(z - y, x));
			slices2 += x;
			cnt += z*x;
		}
	}
	int temp1 = slices1 % m;
	int temp2 = slices2 % m;
	if (temp1 + temp2 > m)
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end());
	sort(arr2.begin(), arr2.end());
	int cnt1 = 0, cnt2 = 0;
	int i = 0;
	while (temp1 > 0)
	{
		cnt1 += min(temp1, arr1[i].second) * arr1[i].first;
		temp1 -= min(temp1, arr1[i].second);
		i++;
	}
	i = 0;
	while (temp2 > 0)
	{
		cnt2 += min(temp2, arr2[i].second) * arr2[i].first;
		temp2 -= min(temp2, arr2[i].second);
		i++;
	}
	cout << cnt - min(cnt1, cnt2);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n; double m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else {
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	int cnt1 = 0, cnt2 = 0;
	int temp1 = (m == 1) ? 1 : slices1 - floor(slices1 / m) * m;
	int temp2 = (m == 1) ? 1 : slices2 - floor(slices2 / m) * m;
	for (int i = 0; i < n; i++)
	{
		if (temp1 > 0 && temp1 > arr3[arr1[i].second])
		{
			cnt1 += arr3[arr1[i].second] * diff[arr1[i].second];
			temp1 -= arr3[arr1[i].second];
		}
		else if (temp1 > 0 && temp1 <= arr3[arr1[i].second])
		{
			cnt1 += temp1*diff[arr1[i].second];
			temp1 = 0;
		}
		if (temp2 > 0 && temp2 > arr3[arr2[i].second])
		{
			cnt2 += arr3[arr2[i].second] * diff[arr2[i].second];
			temp2 -= arr3[arr2[i].second];
		}
		else if (temp2 > 0 && temp2 <= arr3[arr2[i].second])
		{
			cnt2 += temp2*diff[arr2[i].second];
			temp2 = 0;
		}
	}
	if (cnt1 <= cnt2)
	{
		cnt -= cnt1;
		slices1 -= (m == 1) ? 1 : slices1 - floor(slices1 / m) * m;
	}
	else {
		cnt -= cnt2;
		slices2 -= (m == 1) ? 1 : slices2 - floor(slices2 / m) * m;
	}
	cout << cnt;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <stdio.h>
#include <set>
#include <climits>
#include <queue>

using namespace std;

int n; double m;
vector<pair<int, int> > arr1;
vector<pair<int, int> > arr2;
int arr3[100001];
int diff[100001];

bool compare(const pair<int, int>&p1, const pair<int, int>&p2)
{
	if (p1.first == p2.first)
		return diff[p1.second] < diff[p2.second];
	else return p1.first < p2.first;
}

int main()
{
	cin >> n >> m;
	long long cnt = 0, slices = 0, slices1 = 0, slices2 = 0;
	for (int i = 1; i <= n; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		arr3[i] = x;
		diff[i] = abs(y - z);
		slices += x;
		if (y > z)
		{
			arr1.push_back(make_pair(y, i));
			slices1 += x;
			cnt += y*x;
		}
		else{
			arr2.push_back(make_pair(z, i));
			slices2 += x;
			cnt += z*x;
		}
	}
	if (ceil(slices1 / m) + ceil(slices2 / m) == ceil(slices / m))
	{
		cout << cnt;
		return 0;
	}
	sort(arr1.begin(), arr1.end(), compare);
	sort(arr2.begin(), arr2.end(), compare);
	int cnt1 = 0, cnt2 = 0;
	int temp1 = slices1 - floor(slices1 / m) * m;
	int temp2 = slices2 - floor(slices2 / m) * m;
	for (int i = 0; i < n; i++)
	{
		if (temp1 > 0 && temp1 > arr3[arr1[i].second])
		{
			cnt1 += arr3[arr1[i].second] * diff[arr1[i].second];
			temp1 -= arr3[arr1[i].second];
		}
		else if (temp1 > 0 && temp1 <= arr3[arr1[i].second])
		{
			cnt1 += temp1*diff[arr1[i].second];
			temp1 = 0;
		}
		if (temp2 > 0 && temp2 > arr3[arr2[i].second])
		{
			cnt2 += arr3[arr2[i].second] * diff[arr2[i].second];
			temp2 -= arr3[arr2[i].second];
		}
		else if (temp2 > 0 && temp2 <= arr3[arr2[i].second])
		{
			cnt2 += temp2*diff[arr2[i].second];
			temp2 = 0;
		}
		if (temp1 == 0 && temp2 == 0)
			break;
	}
	if (cnt1 <= cnt2 && temp1 <= m- temp2)
		cout << cnt - cnt1;
	else if (cnt2 < cnt1 && temp2 <= m - temp1)
		cout << cnt - cnt2;
	else cout << cnt;
	return 0;
}


def bins(sortedlist,x):
    n=len(sortedlist)
    start = 0
    end = n - 1

    while(start <= end):
        mid =int( (start + end)/2)
        if (x == sortedlist[mid][0]):
            return mid
        elif(x < sortedlist[mid][0]):
            end = mid - 1
        else:
            start = mid + 1

    if(sortedlist[mid][0]<=x):
        return mid
    else:
        return mid-1

n,s=map(int,input().split())
hap=[]

for i in range(n):
    hap.append(list(map(int,input().split())))
a=0
max1=0
b=0
sla=[]
slb=[]
slab=[]
for i in range(n):
    temp=hap[i][0]
    hap[i][0]=hap[i][1]
    hap[i][1]=hap[i][2]
    hap[i][2]=temp
for i in range(n):

    slab.append([hap[i][0]-hap[i][1],hap[i][2]])
happi=0
for i in range(n):
    if(hap[i][0]>hap[i][1]):
        a+=hap[i][2]
        happi+=hap[i][2]*hap[i][0]
    else:
        b+=hap[i][2]
        happi+=hap[i][2]*hap[i][1]
sla.sort()
slb.sort()
slab.sort()
if((a%s +  b%s)>s):
    print(happi)
else:
    loc=bins(slab,0)
    happia=happi
    count=0
    #print(a,b)
    b=b%s
    a=a%s
    left=b%s

    while(left>0):
        if(slab[loc+count][1]<left):
            happia+=slab[loc+count][0]*slab[loc+count][1]
            left-=slab[loc+count][1]
        else:
            happia+=slab[loc+count][0]*left
            break
        count-=1
    left=a%s
    count=0
    happib=happi

    while(slab[loc][0]<=0):
        loc+=1
    #print(slab[loc][0])
    while(left>0):
        if(slab[loc+count][1]<left):
            happib-=slab[loc+count][0]*slab[loc+count][1]
            left-=slab[loc+count][1]
        else:
            happib-=slab[loc+count][0]*left
            break
        count+=1
    #print(happia,happib,happi)
    print(max(happia,happib))


def bins(sortedlist,x):
    n=len(sortedlist)
    start = 0
    end = n - 1

    while(start <= end):
        mid =int( (start + end)/2)
        if (x == sortedlist[mid][0]):
            return mid
        elif(x < sortedlist[mid][0]):
            end = mid - 1
        else:
            start = mid + 1

    if(sortedlist[mid][0]<=x):
        return mid
    else:
        return mid-1

n,s=map(int,input().split())
hap=[]

for i in range(n):
    hap.append(list(map(int,input().split())))
a=0
max1=0
b=0
sla=[]
slb=[]
slab=[]
for i in range(n):
    temp=hap[i][0]
    hap[i][0]=hap[i][1]
    hap[i][1]=hap[i][2]
    hap[i][2]=temp
for i in range(n):

    slab.append([hap[i][0]-hap[i][1],hap[i][2]])
happi=0
for i in range(n):
    if(hap[i][0]>hap[i][1]):
        a+=hap[i][2]
        happi+=hap[i][2]*hap[i][0]
    else:
        b+=hap[i][2]
        happi+=hap[i][2]*hap[i][1]
sla.sort()
slb.sort()
slab.sort()
if((a%s +  b%s)>s):
    print(happi)
else:
    loc=bins(slab,0)
    happia=happi
    count=0
    #print(a,b)
    b=b%s
    a=a%s
    left=b%s

    while(left>0):
        if(slab[loc+count][1]<left):
            happia+=slab[loc+count][0]*slab[loc+count][1]
            left-=slab[loc+count][1]
        else:
            happia+=slab[loc+count][0]*left
            break
        count-=1
    left=a%s
    count=0
    happib=happi

    while(loc<n and slab[loc][0]<=0):
        loc+=1
    #print(slab[loc][0])
    while(left>0):
        if(slab[loc+count][1]<left):
            happib-=slab[loc+count][0]*slab[loc+count][1]
            left-=slab[loc+count][1]
        else:
            happib-=slab[loc+count][0]*left
            break
        count+=1
    #print(happia,happib,happi)
    print(max(happia,happib))


def solve(ls):

    ls.sort(key=lambda q: q[1] - q[0])

    m = sum(si for a, b, si in ls)

    k = s * (m // s)

    n = m - k

    x = y = z = 0

    for a, b, si in ls:

        if k >= si:

            k -= si

            z += si * a

        elif k:

            z += k * a

            x = (si - k) * a

            y = (si - k) * b

            k = 0

        else:

            x += si * a

            y += si * b

    return x, y, z, n

n,s = map(int,input().split())

first=[]

second=[]

for i in range(n):

	si, ai, bi = map(int,input().split())

	if ai>bi:

		first.append((ai,bi,si))

	else:

		second.append((bi,ai,si))

x1,y1,z1,n1 = solve(first)

x2,y2,z2,n2 = solve(second)

d = x1+x2 if n1+n2>s else max(x1+y2,x2+y1)

print(z1+z2+d)

# Made By Mostafa_Khaled


from collections import namedtuple

Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy'])

def happy_diff(m):
    return m.a_happy - m.b_happy

def main():
    input_strings_members, parts = read_input()
    a_members, b_members, other_members = prepare_members(input_strings_members)
    print(count_parts(a_members, b_members, other_members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    a_members = []
    b_members = []
    other_members = []
    for line in input_strings_members:
        m = Member(*map(int, line.split()))
        if happy_diff(m) > 0:
            a_members.append(m)
        elif happy_diff(m) < 0:
            b_members.append(m)
        else:
            other_members.append(m)
    return a_members, b_members, other_members

def count_parts(a_members, b_members, other_members, parts_in_one):
    a_members.sort(key=lambda m: happy_diff(m), reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members.sort(key=lambda m: happy_diff(m), reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        if m.parts + current_modulo <= modulo:
            current_modulo += m.parts
            modulo_members.append(m)
        else:
            remaining_for_modulo_parts = modulo - current_modulo
            if remaining_for_modulo_parts > 0:
                modulo_members.append(Member(remaining_for_modulo_parts, m.a_happy, m.b_happy))
            happy_members.append(Member(m.parts - remaining_for_modulo_parts, m.a_happy, m.b_happy))
            if i < len(members) - 1:
                happy_members.extend(members[i+1:])
            break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = a + other + b

    current_parts = 0
    possible_a_members_happy = 0
    other_b_members_happy = 0
    for i in range(len(last_sorted_members)):
        m = last_sorted_members[i]
        new_current_parts = current_parts + m.parts
        if new_current_parts < parts_in_one:
            possible_a_members_happy += m.parts * m.a_happy
            current_parts = new_current_parts
            continue

        possible_a_members_happy += (parts_in_one - current_parts) * m.a_happy
        if new_current_parts > parts_in_one:
            other_b_members_happy = (new_current_parts - parts_in_one) * m.b_happy
        if i + i < len(last_sorted_members):
            other_b_members_happy += sum(m.parts * m.b_happy for m in last_sorted_members[(i + 1):])
        break

    current_parts = 0
    possible_b_members_happy = 0
    other_a_members_happy = 0
    for i in reversed(range(len(last_sorted_members))):
        m = last_sorted_members[i]
        new_current_parts = current_parts + m.parts
        if new_current_parts < parts_in_one:
            possible_b_members_happy += m.parts * m.b_happy
            current_parts = new_current_parts
            continue

        possible_b_members_happy += (parts_in_one - current_parts) * m.b_happy
        if new_current_parts > parts_in_one:
            other_a_members_happy = (new_current_parts - parts_in_one) * m.a_happy
        if i + i < len(last_sorted_members):
            other_a_members_happy += sum(m.parts * m.a_happy for m in last_sorted_members[:i])
        break

    return max(possible_a_members_happy + other_b_members_happy, possible_b_members_happy + other_a_members_happy)

if __name__ == "__main__":
    main()


class Member:
    def __init__(self, parts, a_happy, b_happy):
        self.parts = parts
        self.a_happy = a_happy
        self.b_happy = b_happy

    @property
    def happy_diff(self):
        return self.a_happy - self.b_happy

    def __repr__(self):
        return '{} {} {}'.format(self.parts, self.a_happy, self.b_happy)

def main():
    input_strings_members, parts = read_input()
    members = prepare_members(input_strings_members)
    print(count_parts(members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    return [Member(*map(int, line.split())) for line in input_strings_members]

def count_parts(members, parts_in_one):
    a_members = list(filter(lambda m: m.happy_diff > 0, members))
    a_members.sort(key=lambda m: m.happy_diff, reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members = list(filter(lambda m: m.happy_diff < 0, members))
    b_members.sort(key=lambda m: m.happy_diff, reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    other_members = list(filter(lambda m: m.happy_diff == 0, members))

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        if m.parts + current_modulo <= modulo:
            current_modulo += m.parts
            modulo_members.append(m)
        else:
            remaining_for_modulo_parts = modulo - current_modulo
            if remaining_for_modulo_parts > 0:
                modulo_members.append(Member(remaining_for_modulo_parts, m.a_happy, m.b_happy))
            happy_members.append(Member(m.parts - remaining_for_modulo_parts, m.a_happy, m.b_happy))
            if i < len(members) - 1:
                happy_members.extend(members[i+1:])
            break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = sorted(a + b + other, key=lambda m: m.happy_diff, reverse=True)
    splitted_members_into_pieces = []
    for m in last_sorted_members:
        splitted_members_into_pieces += [Member(1, m.a_happy, m.b_happy) for i in range(m.parts)]

    last_length = min(len(splitted_members_into_pieces), parts_in_one)

    a_possible_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:last_length])
    other_b_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[last_length:])
    b_possible_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[-last_length:])
    other_a_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:-last_length])
    return max(a_possible_happy + other_b_happy, b_possible_happy + other_a_happy)

main()


from collections import namedtuple

Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy'])

def happy_diff(m):
    return m.a_happy - m.b_happy

def main():
    input_strings_members, parts = read_input()
    a_members, b_members, other_members = prepare_members(input_strings_members)
    print(count_parts(a_members, b_members, other_members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    a_members = []
    b_members = []
    other_members = []
    for line in input_strings_members:
        m = Member(*map(int, line.split()))
        if happy_diff(m) > 0:
            a_members.append(m)
        elif happy_diff(m) < 0:
            b_members.append(m)
        else:
            other_members.append(m)
    return a_members, b_members, other_members

def count_parts(a_members, b_members, other_members, parts_in_one):
    a_members.sort(key=lambda m: happy_diff(m), reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members.sort(key=lambda m: happy_diff(m), reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        if m.parts + current_modulo <= modulo:
            current_modulo += m.parts
            modulo_members.append(m)
        else:
            remaining_for_modulo_parts = modulo - current_modulo
            if remaining_for_modulo_parts > 0:
                modulo_members.append(Member(remaining_for_modulo_parts, m.a_happy, m.b_happy))
            happy_members.append(Member(m.parts - remaining_for_modulo_parts, m.a_happy, m.b_happy))
            if i < len(members) - 1:
                happy_members.extend(members[i+1:])
            break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = a + other + b
    splitted_members_into_pieces = []
    for m in last_sorted_members:
        splitted_members_into_pieces += [Member(1, m.a_happy, m.b_happy) for i in range(m.parts)]

    last_length = min(len(splitted_members_into_pieces), parts_in_one)

    a_possible_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:last_length])
    other_b_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[last_length:])
    b_possible_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[-last_length:])
    other_a_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:-last_length])
    return max(a_possible_happy + other_b_happy, b_possible_happy + other_a_happy)

def test1():
    a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6'])
    assert count_parts(a, b, c, 10) == 314

def test2():
    a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5'])
    assert count_parts(a, b, c, 12) == 84

def test3():
    a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3'])
    assert count_parts(a, b, c, 3) == 16

main()


class Member:
    def __init__(self, parts, a_happy, b_happy):
        self.parts = parts
        self.a_happy = a_happy
        self.b_happy = b_happy

    @property
    def happy_diff(self):
        return self.a_happy - self.b_happy

    def __repr__(self):
        return '{} {} {}'.format(self.parts, self.a_happy, self.b_happy)

def main():
    input_strings_members, parts = read_input()
    members = prepare_members(input_strings_members)
    print(count_parts(members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    return [Member(*map(int, line.split())) for line in input_strings_members]

def count_parts(members, parts_in_one):
    a_members = list(filter(lambda m: m.happy_diff > 0, members))
    a_members.sort(key=lambda m: m.happy_diff, reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members = list(filter(lambda m: m.happy_diff < 0, members))
    b_members.sort(key=lambda m: m.happy_diff, reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    other_members = list(filter(lambda m: m.happy_diff == 0, members))

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        if m.parts + current_modulo <= modulo:
            current_modulo += m.parts
            modulo_members.append(m)
        else:
            remaining_for_modulo_parts = modulo - current_modulo
            if remaining_for_modulo_parts > 0:
                modulo_members.append(Member(remaining_for_modulo_parts, m.a_happy, m.b_happy))
            happy_members.append(Member(m.parts - remaining_for_modulo_parts, m.a_happy, m.b_happy))
            if i < len(members) - 1:
                happy_members.extend(members[i+1:])
            break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = sorted(a + b + other, key=lambda m: m.happy_diff)
    last_length = min(len(last_sorted_members), parts_in_one)
    a_possible_happy = sum(m.parts * m.a_happy for m in last_sorted_members[:last_length])
    b_possible_happy = sum(m.parts * m.b_happy for m in last_sorted_members[-last_length:])
    return max(a_possible_happy, b_possible_happy)

main()


from collections import namedtuple

Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy'])

def happy_diff(m):
    return m.a_happy - m.b_happy

def main():
    input_strings_members, parts = read_input()
    a_members, b_members, other_members = prepare_members(input_strings_members)
    print(count_parts(a_members, b_members, other_members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    a_members = []
    b_members = []
    other_members = []
    for line in input_strings_members:
        m = Member(*map(int, line.split()))
        if happy_diff(m) > 0:
            a_members.append(m)
        elif happy_diff(m) < 0:
            b_members.append(m)
        else:
            other_members.append(m)
    return a_members, b_members, other_members

def count_parts(a_members, b_members, other_members, parts_in_one):
    a_members.sort(key=lambda m: happy_diff(m), reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members.sort(key=lambda m: happy_diff(m), reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        if m.parts + current_modulo <= modulo:
            current_modulo += m.parts
            modulo_members.append(m)
        else:
            remaining_for_modulo_parts = modulo - current_modulo
            if remaining_for_modulo_parts > 0:
                modulo_members.append(Member(remaining_for_modulo_parts, m.a_happy, m.b_happy))
            happy_members.append(Member(m.parts - remaining_for_modulo_parts, m.a_happy, m.b_happy))
            if i < len(members) - 1:
                happy_members.extend(members[i+1:])
            break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = a + other + b

    current_parts = 0
    possible_a_members_happy = 0
    other_b_members_happy = 0
    for i in range(len(last_sorted_members)):
        m = last_sorted_members[i]
        new_current_parts = current_parts + m.parts
        if new_current_parts < parts_in_one:
            possible_a_members_happy += m.parts * m.a_happy
            current_parts = new_current_parts
            continue

        possible_a_members_happy += (parts_in_one - current_parts) * m.a_happy
        if new_current_parts > parts_in_one:
            other_b_members_happy = (new_current_parts - parts_in_one) * m.b_happy
        if i + i < len(last_sorted_members):
            other_b_members_happy += sum(m.parts * m.b_happy for m in last_sorted_members[(i + 1):])
        break

    current_parts = 0
    possible_b_members_happy = 0
    other_a_members_happy = 0
    for i in reversed(range(len(last_sorted_members))):
        m = last_sorted_members[i]
        new_current_parts = current_parts + m.parts
        if new_current_parts < parts_in_one:
            possible_b_members_happy += m.parts * m.b_happy
            current_parts = new_current_parts
            continue

        possible_b_members_happy += (parts_in_one - current_parts) * m.b_happy
        if new_current_parts > parts_in_one:
            other_a_members_happy = (new_current_parts - parts_in_one) * m.a_happy
        if i + i < len(last_sorted_members):
            other_a_members_happy += sum(m.parts * m.a_happy for m in last_sorted_members[:i])
        break

    return max(possible_a_members_happy + other_b_members_happy, possible_b_members_happy + other_a_members_happy)

def test1():
    a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6'])
    assert count_parts(a, b, c, 10) == 314

def test2():
    a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5'])
    assert count_parts(a, b, c, 12) == 84

def test3():
    a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3'])
    assert count_parts(a, b, c, 3) == 16

def test4():
    a, b, c = prepare_members(
        [
            '20000 3 1',
            '20000 2 2',
            '20000 1 3',
            '20000 3 1',
            '20000 2 2',
            '20000 1 3',
            '20000 3 1',
            '20000 2 2',
            '20000 1 3'
        ]
    )
    assert count_parts(a, b, c, 30000)

main()


from collections import namedtuple

Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy'])

def happy_diff(m):
    return m.a_happy - m.b_happy

def main():
    input_strings_members, parts = read_input()
    a_members, b_members, other_members = prepare_members(input_strings_members)
    print(count_parts(a_members, b_members, other_members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    a_members = []
    b_members = []
    other_members = []
    for line in input_strings_members:
        m = Member(*map(int, line.split()))
        if happy_diff(m) > 0:
            a_members.append(m)
        elif happy_diff(m) < 0:
            b_members.append(m)
        else:
            other_members.append(m)
    return a_members, b_members, other_members

def count_parts(a_members, b_members, other_members, parts_in_one):
    a_members.sort(key=lambda m: happy_diff(m), reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members.sort(key=lambda m: happy_diff(m), reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        new_current_modulo = current_modulo + m.parts
        if new_current_modulo < modulo:
            modulo_members.append(m)
            current_modulo = new_current_modulo
            continue

        modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy))
        if new_current_modulo > modulo:
            happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy))
        if (i + 1) < len(members):
            happy_members.extend(members[(i + 1):])
        break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = a + other + b

    if sum(m.parts for m in last_sorted_members) <= parts_in_one:
        return max(
            sum(m.a_happy * m.parts for m in last_sorted_members),
            sum(m.b_happy * m.parts for m in last_sorted_members)
        )

    return sum(m.a_happy * m.parts for m in a + other) + sum(m.b_happy * m.parts for m in b)

def test1():
    a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6'])
    assert count_parts(a, b, c, 10) == 314

def test2():
    a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5'])
    assert count_parts(a, b, c, 12) == 84

def test3():
    a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3'])
    assert count_parts(a, b, c, 3) == 16

def test4():
    a, b, c = prepare_members(
        [
            '2 1 4',
            '2 3 1',
        ]
    )
    assert count_parts(a, b, c, 3) == 14

def test5():
    a, b, c = prepare_members(
        [
            '2 1 2',
            '2 2 1',
            '2 1 3',
            '2 3 1',
            '2 1 4',
            '2 4 1',
        ]
    )
    assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2

def test6():
    a, b, c = prepare_members(
        [
            '2 1 2',
            '2 2 1',
            '2 1 3',
            '2 3 1',
            '2 1 4',
            '2 4 1',
        ]
    )
    assert count_parts(a, b, c, 5) == 16 + 16 + 3

def test_without_last_pizzas():
    a, b, c = prepare_members(
        [
            '3 3 1',
            '3 5 6'
        ]
    )
    assert count_parts(a, b, c, 3) == 27

def test_with_one_last_pizza():
    a, b, c = prepare_members(
        [
            '2 3 1',
            '1 5 6'
        ]
    )
    assert count_parts(a, b, c, 3) == 11

def test_with_two_last_pizzas():
    a, b, c = prepare_members(
        [
            '2 3 4',
            '2 3 1',
            '1 1 1'
        ]
    )
    assert count_parts(a, b, c, 3) == 15

def test_without_different_happies():
    a, b, c = prepare_members(
        [
            '2 2 2',
            '4 1 1',
            '5 6 6'
        ]
    )
    assert count_parts(a, b, c, 3) == 38

if __name__ == "__main__":
    main()


from collections import namedtuple

Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy'])

def happy_diff(m):
    return m.a_happy - m.b_happy

def main():
    input_strings_members, parts = read_input()
    a_members, b_members, other_members = prepare_members(input_strings_members)
    print(count_parts(a_members, b_members, other_members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    a_members = []
    b_members = []
    other_members = []
    for line in input_strings_members:
        m = Member(*map(int, line.split()))
        if happy_diff(m) > 0:
            a_members.append(m)
        elif happy_diff(m) < 0:
            b_members.append(m)
        else:
            other_members.append(m)
    return a_members, b_members, other_members

def count_parts(a_members, b_members, other_members, parts_in_one):
    a_members.sort(key=lambda m: happy_diff(m), reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members.sort(key=lambda m: happy_diff(m), reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        new_current_modulo = current_modulo + m.parts
        if new_current_modulo < modulo:
            modulo_members.append(m)
            current_modulo = new_current_modulo
            continue

        modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy))
        if new_current_modulo > modulo:
            happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy))
        if (i + 1) < len(members):
            happy_members.extend(members[(i + 1):])
        break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = a + other + b

    current_parts = 0
    possible_a_members_happy = 0
    other_b_members_happy = 0
    for i in range(len(last_sorted_members)):
        m = last_sorted_members[i]
        new_current_parts = current_parts + m.parts
        if new_current_parts < parts_in_one:
            possible_a_members_happy += m.parts * m.a_happy
            current_parts = new_current_parts
            continue

        possible_a_members_happy += (parts_in_one - current_parts) * m.a_happy
        if new_current_parts > parts_in_one:
            other_b_members_happy += (new_current_parts - parts_in_one) * m.b_happy
        if (i + i) < len(last_sorted_members):
            other_b_members_happy += sum(m.parts * m.b_happy for m in last_sorted_members[(i + 1):])
        break

    current_parts = 0
    possible_b_members_happy = 0
    other_a_members_happy = 0
    for i in reversed(range(len(last_sorted_members))):
        m = last_sorted_members[i]
        new_current_parts = current_parts + m.parts
        if new_current_parts < parts_in_one:
            possible_b_members_happy += m.parts * m.b_happy
            current_parts = new_current_parts
            continue

        possible_b_members_happy += (parts_in_one - current_parts) * m.b_happy
        if new_current_parts > parts_in_one:
            other_a_members_happy = (new_current_parts - parts_in_one) * m.a_happy
        if i + i < len(last_sorted_members):
            other_a_members_happy += sum(m.parts * m.a_happy for m in last_sorted_members[:i])
        break

    return max(possible_a_members_happy + other_b_members_happy, possible_b_members_happy + other_a_members_happy)

def test1():
    a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6'])
    assert count_parts(a, b, c, 10) == 314

def test2():
    a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5'])
    assert count_parts(a, b, c, 12) == 84

def test3():
    a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3'])
    assert count_parts(a, b, c, 3) == 16

def test4():
    a, b, c = prepare_members(
        [
            '2 1 4',
            '2 3 1',
        ]
    )
    assert count_parts(a, b, c, 3) == 12

def test5():
    a, b, c = prepare_members(
        [
            '2 1 2',
            '2 2 1',
            '2 1 3',
            '2 3 1',
            '2 1 4',
            '2 4 1',
        ]
    )
    assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2

def test6():
    a, b, c = prepare_members(
        [
            '2 1 2',
            '2 2 1',
            '2 1 3',
            '2 3 1',
            '2 1 4',
            '2 4 1',
        ]
    )
    assert count_parts(a, b, c, 5) == 16 + 16 + 3

def test_without_last_pizzas():
    a, b, c = prepare_members(
        [
            '3 3 1',
            '3 5 6'
        ]
    )
    assert count_parts(a, b, c, 3) == 27

def test_with_one_last_pizza():
    a, b, c = prepare_members(
        [
            '2 3 1',
            '1 5 6'
        ]
    )
    assert count_parts(a, b, c, 3) == 11

def test_with_two_last_pizzas():
    a, b, c = prepare_members(
        [
            '2 3 4',
            '2 3 1',
            '1 1 1'
        ]
    )
    assert count_parts(a, b, c, 3) == 15

def test_without_different_happies():
    a, b, c = prepare_members(
        [
            '2 2 2',
            '4 1 1',
            '5 6 6'
        ]
    )
    assert count_parts(a, b, c, 3) == 38

if __name__ == "__main__":
    main()


class Member:
    def __init__(self, parts, a_happy, b_happy):
        self.parts = parts
        self.a_happy = a_happy
        self.b_happy = b_happy

    @property
    def happy_diff(self):
        return self.a_happy - self.b_happy

    def __repr__(self):
        return '{} {} {}'.format(self.parts, self.a_happy, self.b_happy)

def main():
    input_strings_members, parts = read_input()
    members = prepare_members(input_strings_members)
    print(count_parts(members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    return [Member(*map(int, line.split())) for line in input_strings_members]

def count_parts(members, parts_in_one):
    a_members = list(filter(lambda m: m.happy_diff > 0, members))
    a_members.sort(key=lambda m: m.happy_diff, reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members = list(filter(lambda m: m.happy_diff < 0, members))
    b_members.sort(key=lambda m: m.happy_diff, reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    other_members = list(filter(lambda m: m.happy_diff == 0, members))

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        if m.parts + current_modulo <= modulo:
            current_modulo += m.parts
            modulo_members.append(m)
        else:
            remaining_for_modulo_parts = modulo - current_modulo
            if remaining_for_modulo_parts > 0:
                modulo_members.append(Member(remaining_for_modulo_parts, m.a_happy, m.b_happy))
            happy_members.append(Member(m.parts - remaining_for_modulo_parts, m.a_happy, m.b_happy))
            if i < len(members) - 1:
                happy_members.extend(members[i+1:])
            break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = a + other + b
    splitted_members_into_pieces = []
    for m in last_sorted_members:
        splitted_members_into_pieces += [Member(1, m.a_happy, m.b_happy) for i in range(m.parts)]

    last_length = min(len(splitted_members_into_pieces), parts_in_one)

    a_possible_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:last_length])
    other_b_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[last_length:])
    b_possible_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[-last_length:])
    other_a_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:-last_length])
    return max(a_possible_happy + other_b_happy, b_possible_happy + other_a_happy)

def test1():
    members = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6'])
    assert count_parts(members, 10) == 314

def test2():
    members = prepare_members(['3 5 7', '4 6 7', '5 9 5'])
    assert count_parts(members, 12) == 84

def test3():
    members = prepare_members(['2 3 1', '2 2 2', '2 1 3'])
    assert count_parts(members, 3) == 16

main()


class Member:
    def __init__(self, parts, a_happy, b_happy):
        self.parts = parts
        self.a_happy = a_happy
        self.b_happy = b_happy

    @property
    def happy_diff(self):
        return self.a_happy - self.b_happy

    def __repr__(self):
        return '{} {} {}'.format(self.parts, self.a_happy, self.b_happy)

def main():
    input_strings_members, parts = read_input()
    members = prepare_members(input_strings_members)
    print(count_parts(members, parts))

def read_input():
    counts_str = input().split()
    members_count, parts = map(int, counts_str)
    return [input() for _ in range(members_count)], parts

def prepare_members(input_strings_members):
    return [Member(*map(int, line.split())) for line in input_strings_members]

def count_parts(members, parts_in_one):
    a_members = []
    b_members = []
    other_members = []
    for m in members:
        if m.happy_diff > 0:
            a_members.append(m)
        elif m.happy_diff < 0:
            b_members.append(m)
        else:
            other_members.append(m)

    a_members.sort(key=lambda m: m.happy_diff, reverse=False)
    a_modulo = sum(m.parts for m in a_members) % parts_in_one

    a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo)
    a_happy = sum(m.a_happy * m.parts for m in a_happy_members)

    b_members.sort(key=lambda m: m.happy_diff, reverse=True)
    b_modulo = sum(m.parts for m in b_members) % parts_in_one

    b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo)
    b_happy = sum(m.b_happy * m.parts for m in b_happy_members)

    last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one)

    return a_happy + b_happy + last_happy

def split_members_into_happy_groups(members, modulo):
    modulo_members = []
    happy_members = []
    current_modulo = 0

    for i in range(len(members)):
        m = members[i]
        if m.parts + current_modulo <= modulo:
            current_modulo += m.parts
            modulo_members.append(m)
        else:
            remaining_for_modulo_parts = modulo - current_modulo
            if remaining_for_modulo_parts > 0:
                modulo_members.append(Member(remaining_for_modulo_parts, m.a_happy, m.b_happy))
            happy_members.append(Member(m.parts - remaining_for_modulo_parts, m.a_happy, m.b_happy))
            if i < len(members) - 1:
                happy_members.extend(members[i+1:])
            break

    return modulo_members, happy_members

def count_last_pizzas_happy(a, b, other, parts_in_one):
    last_sorted_members = a + other + b
    splitted_members_into_pieces = []
    for m in last_sorted_members:
        splitted_members_into_pieces += [Member(1, m.a_happy, m.b_happy) for i in range(m.parts)]

    last_length = min(len(splitted_members_into_pieces), parts_in_one)

    a_possible_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:last_length])
    other_b_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[last_length:])
    b_possible_happy = sum(m.parts * m.b_happy for m in splitted_members_into_pieces[-last_length:])
    other_a_happy = sum(m.parts * m.a_happy for m in splitted_members_into_pieces[:-last_length])
    return max(a_possible_happy + other_b_happy, b_possible_happy + other_a_happy)

def test1():
    members = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6'])
    assert count_parts(members, 10) == 314

def test2():
    members = prepare_members(['3 5 7', '4 6 7', '5 9 5'])
    assert count_parts(members, 12) == 84

def test3():
    members = prepare_members(['2 3 1', '2 2 2', '2 1 3'])
    assert count_parts(members, 3) == 16

main()


def main():
    N,S=raw_input().split()
    N=eval(N)
    S=eval(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=eval(contestant[i][0])
        contestant[i][1]=eval(contestant[i][1])
        contestant[i][2]=eval(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]

    contestant=sorted(contestant,key=lambda x:x[3])
    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break
#########
    if contestant[side1][3]<0:
        side1+=1

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=S-type1%S
        type1ToType2=S-type2%S
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def main():
    N,S=raw_input().split()
    N=int(N)
    S=int(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=int(contestant[i][0])
        contestant[i][1]=int(contestant[i][1])
        contestant[i][2]=int(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        index=i
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]
        for j in range(i-1,-1,-1):
            if contestant[index][3]>=contestant[j][3]:
                break
            else:
                contestant[index],contestant[j]=contestant[j],contestant[index]
                index=j
    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]==0:
            side1+=1
        else:
            if contestant[side1][3]<0:
                side1+=1
            break

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=S-type1%S
        type1ToType2=S-type2%S
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def main():
    N,S=raw_input().split()
    N=int(N)
    S=int(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=int(contestant[i][0])
        contestant[i][1]=int(contestant[i][1])
        contestant[i][2]=int(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]

    contestant=sorted(contestant,key=lambda x:x[3])
    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break
#########
    if contestant[side1][3]<0:
        side1+=1

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=S-type1%S
        type1ToType2=S-type2%S
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def main():
    N,S=raw_input().split()
    N=int(N)
    S=int(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=int(contestant[i][0])
        contestant[i][1]=int(contestant[i][1])
        contestant[i][2]=int(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        index=i
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]

    contestant=sorted(contestant,key=lambda x:x[3])
    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break
    if contestant[side1][3]<0:
        side1+=1

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=S-type1%S
        type1ToType2=S-type2%S
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def getFirstInput():
    N,S=raw_input().split()
    return int(N),int(S)

def getContestantInput():
    contestant=[0,0,0,0]
    contestant[0],contestant[1],contestant[2]=map(int,raw_input().split())
    contestant[3]=contestant[1]-contestant[2]
    return contestant

def getSumAndType(contestant,sum,type1,type2):
    if contestant[1]>=contestant[2]: return sum+contestant[0]*contestant[1],type1+contestant[0],type2
    else: return sum+contestant[0]*contestant[2],type1,type2+contestant[0]

def getContestantInf(N):
    contestant=[[0,0,0,0] for x in range(N) ]
    sum,type1,type2=0,0,0
    for i in range(N):
        contestant[i]=getContestantInput()
        sum,type1,type2=getSumAndType(contestant[i],sum,type1,type2)
    return contestant,sum,type1,type2

def getSide(N,contestant):
    side1,side2=0,0
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break

    #if contestant[side1][3]<0:
    #   side1+=1
    return side1,side2

def countMove(type,S):
    return type%S

def movePizza2(contestant,type,side):
    numMove=numMove(type)
    sum=0
    while numMove!=0 and side>=0:
        if numMove>=contestant[side][0]:
            sum+=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side-=1
        else:
            sum+=numMove*contestant[side][3]
            numMove=0
            side-=1
    return sum

def movePizza1(contestant,type,side,S,N):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side<N:
        if numMove>=contestant[side][0]:
            sub-=contestant[side][0]*contestant[side][2]
            numMove-=contestant[side][0]
            side+=1
        else:
            sub-=numMove*contestant[side][2]
            numMove=0
            side+=1
    return sub

def movePizza2(contestant,type,side,S):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side>=0:
        if numMove>=contestant[side][0]:
            sub+=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side-=1
        else:
            sub+=numMove*contestant[side][3]
            numMove=0
            side-=1
    return sub

def main():
    N,S=getFirstInput()
    contestant=[[0,0,0,0] for x in range(N)]
    contestant,sum,type1,type2=getContestantInf(N)
    contestant=sorted(contestant,key=lambda x:x[3])
    side1,side2=getSide(N,contestant)
    sub1=movePizza1(contestant,type2,side1,S,N)
    sub2=movePizza2(contestant,type1,side2,S)
    if abs(sub1)<abs(sub2): print sum-abs(sub1)
    else: print sum-abs(sub2)

main()


def main():
    N,S=raw_input().split()
    N=int(N)
    S=int(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=int(contestant[i][0])
        contestant[i][1]=int(contestant[i][1])
        contestant[i][2]=int(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        index=i
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]

    contestant=sorted(contestant,key=lambda x:x[3])
    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]==0:
            side1+=1
            break
        else:
            side1+=1
            break

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=S-type1%S
        type1ToType2=S-type2%S
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def main():
    N,S=raw_input().split()
    N=int(N)
    S=int(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=int(contestant[i][0])
        contestant[i][1]=int(contestant[i][1])
        contestant[i][2]=int(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        index=i
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]
#        for j in range(i-1,-1,-1):
#            if contestant[index][3]>=contestant[j][3]:
#                break
#            else:
#                contestant[index],contestant[j]=contestant[j],contestant[index]
#                index=j

    contestant=sorted(contestant,key=lambda x:x[3])
    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]==0:
            side1+=1
        else:
            if contestant[side1][3]<0:
                side1+=1
            break

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=S-type1%S
        type1ToType2=S-type2%S
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def getFirstInput():
    N,S=raw_input().split()
    return int(N),int(S)

def getContestantInput():
    contestant=[0,0,0,0]
    contestant[0],contestant[1],contestant[2]=map(int,raw_input().split())
    contestant[3]=contestant[1]-contestant[2]
    return contestant

def getSumAndType(contestant,sum,type1,type2):
    if contestant[1]>=contestant[2]: return sum+contestant[0]*contestant[1],type1+contestant[0],type2
    else: return sum+contestant[0]*contestant[2],type1,type2+contestant[0]

def getContestantInf(N):
    contestant=[[0,0,0,0] for x in range(N) ]
    sum,type1,type2=0,0,0
    for i in range(N):
        contestant[i]=getContestantInput()
        sum,type1,type2=getSumAndType(contestant[i],sum,type1,type2)
    return contestant,sum,type1,type2

def getSide(N,contestant):
    side1,side2=-1,-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break

#    if contestant[side1][3]<0:
#       side1+=1

    return side1,side2

def countMove(type,S):
    return type%S

def movePizza1(contestant,type,side,S,N):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side<N:
        if numMove>=contestant[side][0]:
            sub-=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side+=1
        else:
            sub-=numMove*contestant[side][3]
            numMove=0
            side+=1
    return sub

def movePizza2(contestant,type,side,S):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side>=0:
        if numMove>=contestant[side][0]:
            sub+=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side-=1
        else:
            sub+=numMove*contestant[side][3]
            numMove=0
            side-=1
    return sub

def main():
    N,S=getFirstInput()
    contestant=[[0,0,0,0] for x in range(N)]
    contestant,sum,type1,type2=getContestantInf(N)
    contestant=sorted(contestant,key=lambda x:x[3])
    side1,side2=getSide(N,contestant)
    sub1=movePizza1(contestant,type1,side1,S,N)
    sub2=movePizza2(contestant,type2,side2,S)
    if abs(sub1)<abs(sub2): print sum-abs(sub1)
    else: print sum-abs(sub2)

main()


#Codeforces 865B - Ordering Pizza
from sys import stdin

def getSub(list,numMove):
    i=0
    sub=0
    while numMove!=0:
        if list[i][0]<numMove:
            sub+=list[i][0]*list[i][1]
            numMove-=list[i][0]
        else:
            sub+=numMove*list[i][1]
            break
        i+=1
    return sub
def main():
    N,S=map(int,stdin.readline().split())
    A=[]
    B=[]
    SA=0
    SB=0
    sum=0
    for i in xrange(N):
        s,a,b=map(int,stdin.readline().split())
        if a>=b:
            SA+=s
            A.append([s,a-b])
            sum+=s*a
        else:
            SB+=s
            B.append([s,b-a])
            sum+=s*b
    if SA%S+SB%S>S:
        print sum
    else:
        A=sorted(A,key=lambda x:x[1])
        B=sorted(B,key=lambda x:x[1])
        subA=getSub(A,SA%S)
        subB=getSub(B,SB%S)
        if subA<subB:
            print sum-subA
        else:
            print sum-subB
main()


#Codeforces 865B - Ordering Pizza
from sys import stdin

def getSub(list,numMove):
    i=0
    sub=0
    while numMove!=0:
        if list[i][0]<numMove:
            sub+=list[i][0]*list[i][1]
            numMove-=list[i][0]
        else:
            sub+=numMove*list[i][1]
            break
        i+=1
    return sub
def main():
    N,S=map(int,stdin.readline().split())
    A=[]
    B=[]
    SA=0
    SB=0
    sum=0
    for i in xrange(N):
        s,a,b=map(int,stdin.readline().split())
        if a>=b:
            SA+=s
            A.append([s,a-b])
            sum+=s*a
        else:
            SB+=s
            B.append([s,b-a])
            sum+=s*b
    if SA%S+SB%S>S:
        print sum
    else:
        A.sort(key=lambda x:x[1])
        B.sort(key=lambda x:x[1])
        subA=getSub(A,SA%S)
        subB=getSub(B,SB%S)
        if subA<subB:
            print sum-subA
        else:
            print sum-subB
main()


def getFirstInput():
    N,S=raw_input().split()
    return int(N),int(S)

def getContestantInput():
    contestant=[0,0,0,0]
    contestant[0],contestant[1],contestant[2]=map(int,raw_input().split())
    contestant[3]=contestant[1]-contestant[2]
    return contestant

def getSumAndType(contestant,sum,type1,type2):
    if contestant[1]>=contestant[2]: return sum+contestant[0]*contestant[1],type1+contestant[0],type2
    else: return sum+contestant[0]*contestant[2],type1,type2+contestant[0]

def getContestantInf(N):
    contestant=[[0,0,0,0] for x in range(N) ]
    sum,type1,type2=0,0,0
    for i in range(N):
        contestant[i]=getContestantInput()
        sum,type1,type2=getSumAndType(contestant[i],sum,type1,type2)
    return contestant,sum,type1,type2

def getSide(N,contestant):
    side1,side2=-1,-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break

    #if contestant[side1][3]<0:
    #   side1+=1

    return side1,side2

def countMove(type,S):
    return type%S

def movePizza1(contestant,type,side,S,N):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side<N:
        if numMove>=contestant[side][0]:
            sub-=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side+=1
        else:
            sub-=numMove*contestant[side][3]
            numMove=0
            side+=1
    return sub

def movePizza2(contestant,type,side,S):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side>=0:
        if numMove>=contestant[side][0]:
            sub+=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side-=1
        else:
            sub+=numMove*contestant[side][3]
            numMove=0
            side-=1
    return sub

def main():
    N,S=getFirstInput()
    contestant=[[0,0,0,0] for x in range(N)]
    contestant,sum,type1,type2=getContestantInf(N)
    contestant=sorted(contestant,key=lambda x:x[3])
    side1,side2=getSide(N,contestant)
    sub1=movePizza1(contestant,type1,side1,S,N)
    sub2=movePizza2(contestant,type2,side2,S)
    if type1%S+type2%S>S: print sum
    elif: abs(sub1)<abs(sub2): print sum-abs(sub1)
    else: print sum-abs(sub2)

main()


def getFirstInput():
    N,S=raw_input().split()
    return int(N),int(S)

def getContestantInput():
    contestant=[0,0,0,0]
    contestant[0],contestant[1],contestant[2]=map(int,raw_input().split())
    contestant[3]=contestant[1]-contestant[2]
    return contestant

def getSumAndType(contestant,sum,type1,type2):
    if contestant[1]>=contestant[2]: return sum+contestant[0]*contestant[1],type1+contestant[0],type2
    else: return sum+contestant[0]*contestant[2],type1,type2+contestant[0]

def getContestantInf(N):
    contestant=[[0,0,0,0] for x in range(N) ]
    sum,type1,type2=0,0,0
    for i in range(N):
        contestant[i]=getContestantInput()
        sum,type1,type2=getSumAndType(contestant[i],sum,type1,type2)
    return contestant,sum,type1,type2

def getSide(N,contestant):
    side1,side2=-1,-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break

    if contestant[side1][3]<0:
       side1+=1

    return side1,side2

def countMove(type,S):
    return type%S

def movePizza1(contestant,type,side,S,N):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side<N:
        if numMove>=contestant[side][0]:
            sub-=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side+=1
        else:
            sub-=numMove*contestant[side][3]
            numMove=0
            side+=1
    return sub

def movePizza2(contestant,type,side,S):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side>=0:
        if numMove>=contestant[side][0]:
            sub+=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side-=1
        else:
            sub+=numMove*contestant[side][3]
            numMove=0
            side-=1
    return sub

def main():
    N,S=getFirstInput()
    contestant=[[0,0,0,0] for x in range(N)]
    contestant,sum,type1,type2=getContestantInf(N)
    contestant=sorted(contestant,key=lambda x:x[3])
    side1,side2=getSide(N,contestant)
    sub1=movePizza1(contestant,type1,side1,S,N)
    sub2=movePizza2(contestant,type2,side2,S)
    if abs(sub1)<abs(sub2): print sum-abs(sub1)
    else: print sum-abs(sub2)

main()


def main():
    N,S=raw_input().split()
    N=int(N)
    S=int(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=int(contestant[i][0])
        contestant[i][1]=int(contestant[i][1])
        contestant[i][2]=int(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]

    contestant=sorted(contestant,key=lambda x:x[3])

    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break
    if contestant[side1][3]<0:
        side1+=1

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=0
        type1ToType2=0
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def main():
    N,S=raw_input().split()
    N=int(N)
    S=int(S)
    contestant=[[0,0,0,0] for x in range(N) ]
    sum=0
    type1=0
    type2=0
    for i in range(N):
        contestant[i][0],contestant[i][1],contestant[i][2]=raw_input().split()
        contestant[i][0]=int(contestant[i][0])
        contestant[i][1]=int(contestant[i][1])
        contestant[i][2]=int(contestant[i][2])
        contestant[i][3]=contestant[i][1]-contestant[i][2]
        index=i
        if contestant[i][1]>=contestant[i][2]:
           sum+=contestant[i][0]*contestant[i][1]
           type1+=contestant[i][0]
        else:
           sum+=contestant[i][0]*contestant[i][2]
           type2+=contestant[i][0]

    contestant=sorted(contestant,key=lambda x:x[3])
    side1=-1
    side2=-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break
        if contestant[side1][3]<0:
            side1+=1

    sum2=sum
    if type1%S+type2%S>S:
        type2ToType1=S-type1%S
        type1ToType2=S-type2%S
    else:
        type2ToType1=type2%S
        type1ToType2=type1%S
    while type2ToType1!=0 and side2>=0:
        if type2ToType1>=contestant[side2][0]:
            sum2+=contestant[side2][0]*contestant[side2][3]
            type2ToType1-=contestant[side2][0]
            side2-=1
        else:
            sum2+=type2ToType1*contestant[side2][3]
            type2ToType1=0
            side2-=1
    sum1=sum
    while type1ToType2!=0 and side1<N:
        if type1ToType2>=contestant[side1][0]:
            sum1-=contestant[side1][0]*contestant[side1][3]
            type1ToType2-=contestant[side1][0]
            side1+=1
        else:
            sum1-=type1ToType2*contestant[side1][3]
            type1ToType2=0
            side1+=1

    if sum1>sum2:
        print sum1
    else:
        print sum2
main()


def getFirstInput():
    N,S=raw_input().split()
    return int(N),int(S)

def getContestantInput():
    contestant=[0,0,0,0]
    contestant[0],contestant[1],contestant[2]=map(int,raw_input().split())
    contestant[3]=contestant[1]-contestant[2]
    return contestant

def getSumAndType(contestant,sum,type1,type2):
    if contestant[1]>=contestant[2]: return sum+contestant[0]*contestant[1],type1+contestant[0],type2
    else: return sum+contestant[0]*contestant[2],type1,type2+contestant[0]

def getContestantInf(N):
    contestant=[[0,0,0,0] for x in range(N) ]
    sum,type1,type2=0,0,0
    for i in range(N):
        contestant[i]=getContestantInput()
        sum,type1,type2=getSumAndType(contestant[i],sum,type1,type2)
    return contestant,sum,type1,type2

def getSide(N,contestant):
    side1,side2=-1,-1
    for i in range(N):
        side1+=1
        if contestant[i][3]<0: side2+=1
        elif contestant[i][3]>=0: break
    return side1,side2

def countMove(type,S):
    return type%S

def typeFlag(flag,side,N):
    if flag==1: return side<N
    else: return side>=0

def movePizza(contestant,type,side,S,N,flag):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and typeFlag(flag,side,N):
        if numMove>=contestant[side][0]:
            sub-=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side+=flag
        else:
            sub-=numMove*contestant[side][3]
            numMove=0
            side+=flag
    return sub

def main():
    N,S=getFirstInput()
    contestant=[[0,0,0,0] for x in range(N)]
    contestant,sum,type1,type2=getContestantInf(N)
    contestant=sorted(contestant,key=lambda x:x[3])
    side1,side2=getSide(N,contestant)
    sub1=movePizza(contestant,type1,side1,S,N,1)
    sub2=movePizza(contestant,type2,side2,S,N,-1)
    if type1%S+type2%S>S: print sum
    elif abs(sub1)<abs(sub2): print sum-abs(sub1)
    else: print sum-abs(sub2)

main()


def getFirstInput():
    N,S=raw_input().split()
    return int(N),int(S)

def getContestantInput():
    contestant=[0,0,0,0]
    contestant[0],contestant[1],contestant[2]=map(int,raw_input().split())
    contestant[3]=contestant[1]-contestant[2]
    return contestant

def getSumAndType(contestant,sum,type1,type2):
    if contestant[1]>=contestant[2]: return sum+contestant[0]*contestant[1],type1+contestant[0],type2
    else: return sum+contestant[0]*contestant[2],type1,type2+contestant[0]

def getContestantInf(N):
    contestant=[[0,0,0,0] for x in range(N) ]
    sum,type1,type2=0,0,0
    for i in range(N):
        contestant[i]=getContestantInput()
        sum,type1,type2=getSumAndType(contestant[i],sum,type1,type2)
    return contestant,sum,type1,type2

def getSide(N,contestant):
    side1,side2=-1,-1
    for i in range(N):
        if contestant[i][3]<0:
            side1+=1
            side2+=1
        elif contestant[i][3]>=0:
            side1+=1
            break

    #if contestant[side1][3]<0:
    #   side1+=1

    return side1,side2

def countMove(type,S):
    return type%S

def movePizza1(contestant,type,side,S,N):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side<N:
        if numMove>=contestant[side][0]:
            sub-=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side+=1
        else:
            sub-=numMove*contestant[side][3]
            numMove=0
            side+=1
    return sub

def movePizza2(contestant,type,side,S):
    numMove=countMove(type,S)
    sub=0
    while numMove!=0 and side>=0:
        if numMove>=contestant[side][0]:
            sub+=contestant[side][0]*contestant[side][3]
            numMove-=contestant[side][0]
            side-=1
        else:
            sub+=numMove*contestant[side][3]
            numMove=0
            side-=1
    return sub

def main():
    N,S=getFirstInput()
    contestant=[[0,0,0,0] for x in range(N)]
    contestant,sum,type1,type2=getContestantInf(N)
    contestant=sorted(contestant,key=lambda x:x[3])
    side1,side2=getSide(N,contestant)
    sub1=movePizza1(contestant,type1,side1,S,N)
    sub2=movePizza2(contestant,type2,side2,S)
    if type1%S+type2%S>S: print sum
    elif abs(sub1)<abs(sub2): print sum-abs(sub1)
    else: print sum-abs(sub2)

main()


from sys import stdin
from collections import deque

n,S = [int(x) for x in stdin.readline().split()]

ppl = []

base = 0

tSlices = 0

for dude in range(n):
    s,a,b = [int(x) for x in stdin.readline().split()]
    base += s*a
    tSlices += s
    ppl.append([b-a,s])
#print(base)

if tSlices % S != 0:
    ppl.append([0, S-(tSlices%S)])

ppl.sort(reverse=True)

totalS = 0
totalH = 0

for h,s in ppl:
    if totalS + s < S:
        totalS += s
        totalH += h*s
    else:
        s -= S-totalS
        totalH += h*(S-totalS)
        if totalH > 0:
            base += totalH
        else:
            break
        totalS = 0
        totalH = 0
        if h > 0:
            base += h*((s//S)*S)
            totalS = s%S
            totalH = h*(s%S)
        else:
            break

print(base)


from sys import stdin
from collections import deque

n,S = [int(x) for x in stdin.readline().split()]

ppl = []

base = 0

tSlices = 0

for dude in range(n):
    s,a,b = [int(x) for x in stdin.readline().split()]
    base += s*a
    tSlices += s
    ppl.append([b-a,s])
#print(base)

if tSlices % S != 0:
    ppl.append([0, S-(tSlices%S)])

ppl.sort(reverse=True)

totalS = 0
totalH = 0

for h,s in ppl:
    if totalS + s < S:
        totalS += s
        totalH += h*s
    else:
        s -= S-totalS
        totalH += h*(S-totalS)
        if totalH > 0:
            base += totalH
        else:
            break
        totalS = 0
        totalH = 0
        if h > 0:
            base += h*(s//S)
            totalS = s%S
            totalH = h*(s%S)
        else:
            break

print(base)