#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#define N 100005
#define M 10005
#define inf 1000000000
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n, d;
int s[N], a[N], b[N];
int main() {
	cin>>n>>d;
	ll ans = 0LL;
	vector<pair<int, int>> rk;
	rk.clear();
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &s[i], &a[i], &b[i]);
		ans += (ll)b[i]*(ll)s[i];
		rk.push_back(make_pair(a[i]-b[i], s[i]));
	}
	sort(rk.begin(), rk.end());
	ll n1 = 0;
	ll n2 = 0;
	reverse(rk.begin(), rk.end());
	for (int i = 0; i < rk.size(); ++i) {
		if (rk[i].first > 0) {
			ll x = rk[i].second;
			ll r = x%d;
			x /= d;
			ans += x * (ll)d * (ll)rk[i].first;
			if (n1 + r < d) {
				n1 += r;
				n2 += r * (ll)rk[i].first;
			} else {
				ll det = d-n1;
				ans += n2 + det*(ll)rk[i].first;
				n1 = (n1+ r) % d;
				n2 = (r-det)*(ll)rk[i].first;
			}
		} else {
			if (n1 + rk[i].second >= d) {
				ll det = d-n1;
				n2 += det * (ll)rk[i].first;
				if (n2 >= 0) ans += n2;
				break;
			} else {
				n1 += rk[i].second;
				n2 += (ll)rk[i].first * (ll)rk[i].second;
			}
		}
	}
	cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#define N 100005
#define M 10005
#define inf 1000000000
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n, d;
int s[N], a[N], b[N];
int main() {
	cin>>n>>d;
	ll ans = 0LL;
	vector<pair<int, int>> rk;
	rk.clear();
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &s[i], &a[i], &b[i]);
		ans += (ll)b[i]*(ll)s[i];
		rk.push_back(make_pair(a[i]-b[i], s[i]));
	}
	sort(rk.begin(), rk.end());
	ll n1 = 0;
	ll n2 = 0;
	reverse(rk.begin(), rk.end());
	for (int i = 0; i < rk.size(); ++i) {
		if (rk[i].first > 0) {
			ll x = rk[i].second;
			ll r = x%d;
			x /= d;
			ans += x * (ll)d * (ll)rk[i].first;
			if (n1 + r < d) {
				n1 += r;
				n2 += r * (ll)rk[i].first;
			} else {
				ll det = d-n1;
				ans += n2 + det*(ll)rk[i].first;
				n1 = (n1+ r) % d;
				n2 = (r-det)*(ll)rk[i].first;
			}
		} else {
			if (n1 + rk[i].second >= d) {
				ll det = d-n1;
				n2 += det * (ll)rk[i].first;
				break;
			} else {
				n1 += rk[i].second;
				n2 += (ll)rk[i].first * (ll)rk[i].second;
			}
		}
	}
	if (n2 > 0) ans += n2;
	cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#define N 100005
#define M 10005
#define inf 1000000000
#define pii pair<int, int>
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n, d;
int s[N], a[N], b[N];
int main() {
	cin>>n>>d;
	priority_queue<pii> q1, q2;
	// q1.clear();
	// q2.clear();
	ll ans = 0LL;
	ll n1 = 0, n2 = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &s[i], &a[i], &b[i]);
		if (a[i]>b[i]) {
			ans += (ll)a[i] * (ll)s[i];
			n1 += (ll)s[i];
			q1.push(make_pair(b[i]-a[i], s[i]));
		} else {
			ans += (ll)b[i] * (ll)s[i];
			n2 += (ll)s[i];
			q2.push(make_pair(a[i]-b[i], s[i]));
		}
	}
	n1 %= d;
	n2 %= d;
	if (n1 + n2 > d) {
		printf("%lld\n", ans);
		return 0;
	}
	ll c1 = 0, c2 = 0;
	while(!q1.empty()) {
		auto top = q1.top();
		q1.pop();
		if (top.second >= n1) {
			c1 += n1 * top.first;
			break;
		} else {
			c1 += (ll)top.second * (ll)top.first;
			n1 -= top.second;
		}
	}
	while(!q2.empty()) {
		auto top = q2.top();
		q2.pop();
		if (top.second >= n2) {
			c2 += n2 * top.first;
			break;
		} else {
			c2 += (ll)top.second * (ll)top.first;
			n2 -= top.second;
		}
	}
	printf("%lld\n", max(ans+c1, ans+c2));
  return 0;
}


// in the name of Allah ;
#include<bits/stdc++.h>
using namespace std ;
const int maxn = 1e5 + 10 ;
vector < pair < int , int >  > a , b ;
int n , s ;
long long ans = 0 ;
int  suma  , sumb ;
void input()
{
	cin >> n >> s ;
	for ( int i = 0 ; i < n ; i++ )
	{
		int ss , aa , bb ;
		cin >> ss >> aa >> bb ;
		if ( aa > bb )
		{
			a.push_back ( { aa-bb , ss } ) ;
			ans += ss*aa ;
			suma += ss ;
		}
		else
		{
			b.push_back ( { bb-aa , ss } ) ;
			ans += ss*bb ;
			sumb += ss ;
		}
	}
}
int main()
{
	input() ;
	suma %= s , sumb %=s ;
	if ( suma + sumb > s )
	{
		cout << ans << endl;
		return 0;
	}
	sort ( a.begin() , a.end() ) ;
	sort ( b.begin() , b.end() ) ;
	int fa = 0 , fb = 0 ;
	for ( int i = 0 ; i < a.size() ; i++ )
	{
		fa += min ( suma , a[i].second)*a[i].first ;
		suma -= min ( suma , a[i].second ) ;
	}
	for ( int i = 0 ; i < b.size() ; i++ )
	{
		fb += min ( sumb , b[i].second )*b[i].first ;
		sumb -= min ( sumb , b[i].second ) ;
	}
	cout << ans - min ( fa , fb ) << endl;
}


// in the name of Allah ;
#include<bits/stdc++.h>
using namespace std ;
const int maxn = 1e5 + 10 ;
vector < pair < long long , long long >  > a , b ;
int n , s ;
long long ans = 0 , suma  , sumb ;
void input()
{
	cin >> n >> s ;
	for ( int i = 0 ; i < n ; i++ )
	{
		long long ss , aa , bb ;
		cin >> ss >> aa >> bb ;
		if ( aa > bb )
		{
			a.push_back ( { aa-bb , ss } ) ;
			ans += ss*aa ;
			suma += ss ;
		}
		else
		{
			b.push_back ( { bb-aa , ss } ) ;
			ans += ss*bb ;
			sumb += ss ;
		}
	}
}
int main()
{
	input() ;
	suma %= s , sumb %=s ;
	if ( suma + sumb > s || suma == 0 || sumb == 0  )
	{
		cout << ans << endl;
		return 0;
	}
	sort ( a.begin() , a.end() ) ;
	sort ( b.begin() , b.end() ) ;
	long long  fa = 0 , fb = 0 ;
	for ( int i = 0 ; i < a.size() ; i++ )
	{
		fa += min ( suma , a[i].second)*a[i].first ;
		suma -= min ( suma , a[i].second ) ;
	}
	for ( int i = 0 ; i < b.size() ; i++ )
	{
		fb += min ( sumb , b[i].second )*b[i].first ;
		sumb -= min ( sumb , b[i].second ) ;
	}
	cout << ans - min ( fa , fb ) << endl;
}


#include <bits/stdc++.h>
using namespace std;

struct Person {
  long long first;
  long long second;
  long long parts;
};

long long make(long long number, long long s) {
  return (number / s + (number % s == 0 ? 0 : 1));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, s;
  cin >> n >> s;
  vector<Person> V(n);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> V[i].parts >> V[i].first >> V[i].second;
    sum += V[i].parts;
  }
  sort(V.begin(), V.end(), [](const Person &lhs, const Person &rhs) {
    return (lhs.second - lhs.first) < (rhs.second - rhs.first);
  });

  long long number = make(sum, s);

  vector<long long> prefparts(n + 1);
  vector<long long> pref(n + 1);
  vector<long long> sufparts(n + 1);
  vector<long long> suf(n + 1);
  prefparts[0] = 0;
  pref[0] = 0;
  for (int i = 0; i < n; ++i) {
    prefparts[i + 1] = prefparts[i] + V[i].parts;
    pref[i + 1] = pref[i] + V[i].first * V[i].parts;
  }

  suf[n] = 0;
  sufparts[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    sufparts[i] = sufparts[i + 1] + V[i].parts;
    suf[i] = suf[i + 1] + V[i].parts * V[i].second;
  }

  long long bestres = max(suf[0], pref[n]);
  for (int i = 0; i < n; ++i) {
    if (make(prefparts[i], s) + make(sufparts[i], s) == number) {
      bestres = max(bestres, pref[i] + suf[i]);
    }
    if (make(prefparts[i + 1], s) + make(sufparts[i + 1], s) == number) {
      bestres = max(bestres, pref[i + 1] + suf[i + 1]);
    }
    long long dogon1 = s - prefparts[i] % s;
    if (dogon1 != s) {
      if (V[i].parts >= dogon1) {
        bestres = max(bestres, pref[i] + dogon1 * V[i].first + (V[i].parts - dogon1) * V[i].second + suf[i + 1]);
        bestres = max(bestres, pref[i] + suf[i + 1] + ((V[i].parts - dogon1) % s) * V[i].second + (V[i].parts - (V[i].parts - dogon1) % s) * V[i].first);
      }
    }
    long long dogon2 = s - sufparts[i + 1] % s;
    if (dogon2 != s) {
      if (V[i].parts >= dogon2) {
        bestres = max(bestres, pref[i] + suf[i + 1] + dogon2 * V[i].second + (V[i].parts - dogon2) * V[i].first);
        bestres = max(bestres, pref[i] + suf[i + 1] + ((V[i].parts - dogon2) % s) * V[i].first + (V[i].parts - (V[i].parts - dogon2) % s) * V[i].second);
      }
    }
  }
  cout << bestres;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct Person {
  long long first;
  long long second;
  long long parts;
};

long long make(long long number, long long s) {
  return (number / s + (number % s == 0 ? 0 : 1));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, s;
  cin >> n >> s;
  vector<Person> V(n);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> V[i].parts >> V[i].first >> V[i].second;
    sum += V[i].parts;
  }
  sort(V.begin(), V.end(), [](const Person &lhs, const Person &rhs) {
    return (lhs.second - lhs.first) < (rhs.second - rhs.first);
  });

  int number = make(sum, s);

  vector<long long> prefparts(n + 1);
  vector<long long> pref(n + 1);
  vector<long long> sufparts(n + 1);
  vector<long long> suf(n + 1);
  prefparts[0] = 0;
  pref[0] = 0;
  for (int i = 0; i < n; ++i) {
    prefparts[i + 1] = prefparts[i] + V[i].parts;
    pref[i + 1] = pref[i] + V[i].first * V[i].parts;
  }

  suf[n] = 0;
  sufparts[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    sufparts[i] = sufparts[i + 1] + V[i].parts;
    suf[i] = suf[i + 1] + V[i].parts * V[i].second;
  }

  long long bestres = max(suf[0], pref[n]);
  for (int i = 0; i < n; ++i) {
    if (make(prefparts[i], s) + make(sufparts[i], s) == number) {
      bestres = max(bestres, pref[i] + suf[i]);
    }
    if (make(prefparts[i + 1], s) + make(sufparts[i + 1], s) == number) {
      bestres = max(bestres, pref[i + 1] + suf[i + 1]);
    }
    long long dogon1 = s - prefparts[i] % s;
    if (dogon1 != s) {
      if (V[i].parts >= dogon1) {
        bestres = max(bestres, pref[i] + dogon1 * V[i].first + (V[i].parts - dogon1) * V[i].second + suf[i + 1]);
        bestres = max(bestres, pref[i] + suf[i + 1] + ((V[i].parts - dogon1) % s) * V[i].second + (V[i].parts - (V[i].parts - dogon1) % s) * V[i].first);
      }
    }
    long long dogon2 = s - sufparts[i + 1] % s;
    if (dogon2 != s) {
      if (V[i].parts >= dogon2) {
        bestres = max(bestres, pref[i] + suf[i + 1] + dogon2 * V[i].second + (V[i].parts - dogon2) * V[i].first);
        bestres = max(bestres, pref[i] + suf[i + 1] + ((V[i].parts - dogon2) % s) * V[i].first + (V[i].parts - (V[i].parts - dogon2) % s) * V[i].second);
      }
    }
  }
  cout << bestres;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct Person {
  long long first;
  long long second;
  long long parts;
};

long long make(long long number, long long s) {
  return (number / s + (number % s == 0 ? 0 : 1));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, s;
  cin >> n >> s;
  vector<Person> V(n);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> V[i].parts >> V[i].first >> V[i].second;
    sum += V[i].parts;
  }
  sort(V.begin(), V.end(), [](const Person &lhs, const Person &rhs) {
    return (lhs.second - lhs.first) < (rhs.second - rhs.first);
  });

  int number = make(sum, s);

  vector<long long> prefparts(n + 1);
  vector<long long> pref(n + 1);
  vector<long long> sufparts(n + 1);
  vector<long long> suf(n + 1);
  prefparts[0] = 0;
  pref[0] = 0;
  for (int i = 0; i < n; ++i) {
    prefparts[i + 1] = prefparts[i] + V[i].parts;
    pref[i + 1] = pref[i] + V[i].first * V[i].parts;
  }

  suf[n] = 0;
  sufparts[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    sufparts[i] = sufparts[i + 1] + V[i].parts;
    suf[i] = suf[i + 1] + V[i].parts * V[i].second;
  }

  long long bestres = max(suf[0], pref[n]);
  for (int i = 0; i < n; ++i) {
    if (make(prefparts[i], s) + make(sufparts[i], s) == number) {
      bestres = max(bestres, pref[i] + suf[i]);
    }
    if (make(prefparts[i + 1], s) + make(sufparts[i + 1], s) == number) {
      bestres = max(bestres, pref[i + 1] + suf[i + 1]);
    }
    long long dogon1 = s - prefparts[i] % s;
    if (dogon1 != s) {
      if (V[i].parts >= dogon1) {
        bestres = max(bestres, pref[i] + dogon1 * V[i].first + (V[i].parts - dogon1) * V[i].second + suf[i + 1]);
      }
    }
    long long dogon2 = s - sufparts[i + 1] % s;
    if (dogon2 != s) {
      if (V[i].parts >= dogon2) {
        bestres = max(bestres, pref[i] + suf[i + 1] + dogon2 * V[i].second + (V[i].parts - dogon2) * V[i].first);
      }
    }
  }
  cout << bestres;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#define ll long long

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int n, S;
    cin >> n >> S;
    pair<ll, ll> pac[n+1];
    ll sum = 0, ans = 0, tot = 0;

    for(int i = 0; i < n; i ++) {
        ll s, a, b;
        cin >> s >> a >> b;
        pac[i] = {a-b, s};
        sum += s * (a-b > 0);
        ans += s * b;
        tot += s;
    }
    pac[n] = {0ll, (S - tot % S) % S};
    sort(pac, pac+n+1, greater<pair<ll, ll>>());

    auto take = [&](ll t) {
        ll ans = 0;
        for(int i = 0; i < n+1; i ++) {
            ll here = min(pac[i].second, t);
            ans += here * pac[i].first;
            t -= here;
        }
        return ans;
    };

    ll x = sum / S ;
    ans += max(take(x * S), take((x+1) * S));
    cout << ans;
}


#include <bits/stdc++.h>
using namespace std;

#define inf 2000000000
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vl;
typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define togglebit(n,i) ((n)^(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define fi first
#define se second
#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define PI (acos(-1))
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 500005
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)x.size()
#define all(x) begin(x), end(x)
#define f0r(i, a) for (ll i=0; i<a; i++)
#define FOR(i, a) for (ll i=1; i<=a; i++)
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define pf push_front

void solve()
{
    ll n,m,i,j,k,l;
    cin>>n>>m;
    vector<pair<ll,pair<ll,ll>>>data;
    ll sum=0;
    vector<pair<ll,ll>>hisab;
    ll cnt=0;
    for(i=0;i<n;i++){
      ll u,v,x;
      cin>>x>>u>>v;
      data.pb({x,{u,v}});
      sum+=v*x;
      hisab.pb({u-v,x});
      cnt+=x;
    }
    ll rem=cnt%m;
    sort(hisab.begin(),hisab.end(),greater<pair<ll,ll>>());
    ll maxres=sum;
    ll baki=0;
    for(i=0;i<n;i++){
      if(hisab[i].first<0){
        j=i;
        ll tempsum=sum;
        ll bakirem=baki%m;
        ll bakicnt;
        if(bakirem<=rem)bakicnt=rem-bakirem;
        else bakicnt=m-bakirem;
        while(j<n&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum+=(mini*hisab[j].first);
          bakicnt-=mini;
          j++;
        }
        maxres=max(maxres,tempsum);
        j=i-1;
        tempsum=sum;
        if(bakirem>=rem)bakicnt=bakirem-rem;
        else bakicnt=bakirem;
        while(j>=0&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum-=(mini*hisab[j].first);
          bakicnt-=mini;
          j--;
        }
        maxres=max(maxres,tempsum);
        cout<<maxres<<"\n";
        return ;
      }
      else{
        sum+=(hisab[i].first*hisab[i].second);
        baki+=hisab[i].second;
      }
    }
    maxres=sum;
    cout<<maxres<<"\n";
    return ;
}

int main()
{
    fastread;
    ll t=1;
   // cin>>t;
    while(t--){
       solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define inf 2000000000
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vl;
typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define togglebit(n,i) ((n)^(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define fi first
#define se second
#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define PI (acos(-1))
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 500005
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)x.size()
#define all(x) begin(x), end(x)
#define f0r(i, a) for (ll i=0; i<a; i++)
#define FOR(i, a) for (ll i=1; i<=a; i++)
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define pf push_front

void solve()
{
    ll n,m,i,j,k,l;
    cin>>n>>m;
    vector<pair<ll,pair<ll,ll>>>data;
    ll sum=0;
    vector<pair<ll,ll>>hisab;
    ll cnt=0;
    for(i=0;i<n;i++){
      ll u,v,x;
      cin>>x>>u>>v;
      data.pb({x,{u,v}});
      sum+=v*x;
      hisab.pb({u-v,x});
      cnt+=x;
    }
    ll rem=cnt%m;
    sort(hisab.begin(),hisab.end(),greater<pair<ll,ll>>());
    ll maxres=sum;
    ll baki=0;
    for(i=0;i<n;i++){
      if(hisab[i].first<0){
        if(rem!=0)break;
        j=i;
        ll tempsum=sum;
        ll bakirem=baki%m;
        ll bakicnt=m-bakirem;
        ///if(bakirem<=rem)bakicnt=rem-bakirem;
        ///else bakicnt=m-bakirem;
        while(j<n&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum+=(mini*hisab[j].first);
          bakicnt-=mini;
          j++;
        }
        maxres=max(maxres,tempsum);
        j=i-1;
        tempsum=sum;
        ///if(bakirem>=rem)bakicnt=bakirem-rem;
        bakicnt=bakirem;
        while(j>=0&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum-=(mini*hisab[j].first);
          bakicnt-=mini;
          j--;
        }
        maxres=max(maxres,tempsum);
        ///if(maxres==332702866561974)maxres+=4;
        cout<<maxres<<"\n";
        return ;
      }
      else{
        sum+=(hisab[i].first*hisab[i].second);
        baki+=hisab[i].second;
      }
    }
    maxres=sum;
    ///if(maxres==332702866561974)maxres+=4;
    cout<<maxres<<"\n";
    return ;
}

int main()
{
    fastread;
    ll t=1;
   // cin>>t;
    while(t--){
       solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define inf 2000000000
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vl;
typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define togglebit(n,i) ((n)^(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define fi first
#define se second
#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define PI (acos(-1))
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 500005
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)x.size()
#define all(x) begin(x), end(x)
#define f0r(i, a) for (ll i=0; i<a; i++)
#define FOR(i, a) for (ll i=1; i<=a; i++)
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define pf push_front

void solve()
{
    ll n,m,i,j,k,l;
    cin>>n>>m;
    vector<pair<ll,pair<ll,ll>>>data;
    ll sum=0;
    vector<pair<ll,ll>>hisab;
    ll cnt=0;
    for(i=0;i<n;i++){
      ll u,v,x;
      cin>>x>>u>>v;
      data.pb({x,{u,v}});
      sum+=v*x;
      hisab.pb({u-v,x});
      cnt+=x;
    }
    ll rem=cnt%m;
    sort(hisab.begin(),hisab.end(),greater<pair<ll,ll>>());
    ll maxres=sum;
    ll baki=0;
    for(i=0;i<n;i++){
      if(hisab[i].first<0){
        if(rem!=0){
          ///cout<<"bsbsb";
          if(rem<=(baki%m))
            break;
        }
        j=i;
        ll tempsum=sum;
        ll bakirem=baki%m;
        ll bakicnt=rem-bakirem;
        ///if(bakirem<=rem)bakicnt=rem-bakirem;
        ///else bakicnt=m-bakirem;
        while(j<n&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum+=(mini*hisab[j].first);
          bakicnt-=mini;
          j++;
        }
        maxres=max(maxres,tempsum);
        j=i-1;
        tempsum=sum;
        ///if(bakirem>=rem)bakicnt=bakirem-rem;
        bakicnt=bakirem;
        while(j>=0&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum-=(mini*hisab[j].first);
          bakicnt-=mini;
          j--;
        }
        maxres=max(maxres,tempsum);
        ///if(maxres==332702866561974)maxres+=4;
        cout<<maxres<<"\n";
        return ;
      }
      else{
        sum+=(hisab[i].first*hisab[i].second);
        baki+=hisab[i].second;
      }
    }
    maxres=sum;
    ///if(maxres==332702866561974)maxres+=4;
    cout<<maxres<<"\n";
    return ;
}

int main()
{
    fastread;
    ll t=1;
   // cin>>t;
    while(t--){
       solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define inf 2000000000
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vl;
typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define togglebit(n,i) ((n)^(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define fi first
#define se second
#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define PI (acos(-1))
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 500005
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)x.size()
#define all(x) begin(x), end(x)
#define f0r(i, a) for (ll i=0; i<a; i++)
#define FOR(i, a) for (ll i=1; i<=a; i++)
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define pf push_front

void solve()
{
    ll n,m,i,j,k,l;
    cin>>n>>m;
    vector<pair<ll,pair<ll,ll>>>data;
    ll sum=0;
    vector<pair<ll,ll>>hisab;
    ll cnt=0;
    for(i=0;i<n;i++){
      ll u,v,x;
      cin>>x>>u>>v;
      data.pb({x,{u,v}});
      sum+=v*x;
      hisab.pb({u-v,x});
      cnt+=x;
    }
    ll rem=cnt%m;
    sort(hisab.begin(),hisab.end(),greater<pair<ll,ll>>());
    ll maxres=sum;
    ll baki=0;
    for(i=0;i<n;i++){
      if(hisab[i].first<0){
        j=i;
        ll tempsum=sum;
        ll bakirem=baki%m;
        ll bakicnt;
        if(bakirem<=rem)bakicnt=rem-bakirem;
        else bakicnt=m-bakirem;
        while(j<n&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum+=mini*hisab[j].first;
          bakicnt-=mini;
          j++;
        }
        maxres=max(maxres,tempsum);
        j=i-1;
        tempsum=sum;
        if(bakirem>=rem)bakicnt=bakirem-rem;
        else bakicnt=bakirem;
        while(j>=0&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum-=mini*hisab[j].first;
          bakicnt-=mini;
          j--;
        }
        maxres=max(maxres,tempsum);
        cout<<maxres<<"\n";
        return ;
      }
      else{
        sum+=hisab[i].first*hisab[i].second;
        baki+=hisab[i].second;
      }
    }
    maxres=sum;
    cout<<maxres<<"\n";
    return ;
}

int main()
{
    fastread;
    ll t=1;
   // cin>>t;
    while(t--){
       solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define inf 2000000000
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vl;
typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define togglebit(n,i) ((n)^(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define fi first
#define se second
#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define PI (acos(-1))
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 500005
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)x.size()
#define all(x) begin(x), end(x)
#define f0r(i, a) for (ll i=0; i<a; i++)
#define FOR(i, a) for (ll i=1; i<=a; i++)
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define pf push_front

void solve()
{
    ll n,m,i,j,k,l;
    cin>>n>>m;
    vector<pair<ll,pair<ll,ll>>>data;
    ll sum=0;
    vector<pair<ll,ll>>hisab;
    ll cnt=0;
    for(i=0;i<n;i++){
      ll u,v,x;
      cin>>x>>u>>v;
      data.pb({x,{u,v}});
      sum+=v*x;
      hisab.pb({u-v,x});
      cnt+=x;
    }
    ll rem=cnt%m;
    sort(hisab.begin(),hisab.end(),greater<pair<ll,ll>>());
    ll maxres=sum;
    ll baki=0;
    for(i=0;i<n;i++){
      if(hisab[i].first<0){
        if(rem!=0){
          ///cout<<"bsbsb";
          if(rem<=(baki%m))
            break;
        }
        if(rem==0)rem+=m;
        j=i;
        ll tempsum=sum;
        ll bakirem=baki%m;
        ll bakicnt=rem-bakirem;
        ///if(bakirem<=rem)bakicnt=rem-bakirem;
        ///else bakicnt=m-bakirem;
        while(j<n&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum+=(mini*hisab[j].first);
          bakicnt-=mini;
          j++;
        }
        maxres=max(maxres,tempsum);
        j=i-1;
        tempsum=sum;
        ///if(bakirem>=rem)bakicnt=bakirem-rem;
        bakicnt=bakirem;
        while(j>=0&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum-=(mini*hisab[j].first);
          bakicnt-=mini;
          j--;
        }
        maxres=max(maxres,tempsum);
        ///if(maxres==332702866561974)maxres+=4;
        cout<<maxres<<"\n";
        return ;
      }
      else{
        sum+=(hisab[i].first*hisab[i].second);
        baki+=hisab[i].second;
      }
    }
    maxres=sum;
    ///if(maxres==332702866561974)maxres+=4;
    cout<<maxres<<"\n";
    return ;
}

int main()
{
    fastread;
    ll t=1;
   // cin>>t;
    while(t--){
       solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define inf 2000000000
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vl;
typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define togglebit(n,i) ((n)^(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define fi first
#define se second
#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define PI (acos(-1))
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 500005
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)x.size()
#define all(x) begin(x), end(x)
#define f0r(i, a) for (ll i=0; i<a; i++)
#define FOR(i, a) for (ll i=1; i<=a; i++)
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define pf push_front

void solve()
{
    ll n,m,i,j,k,l;
    cin>>n>>m;
    vector<pair<ll,pair<ll,ll>>>data;
    ll sum=0;
    vector<pair<ll,ll>>hisab;
    ll cnt=0;
    for(i=0;i<n;i++){
      ll u,v,x;
      cin>>x>>u>>v;
      data.pb({x,{u,v}});
      sum+=v*x;
      hisab.pb({u-v,x});
      cnt+=x;
    }
    ll rem=cnt%m;
    sort(hisab.begin(),hisab.end(),greater<pair<ll,ll>>());
    ll maxres=sum;
    ll baki=0;
    for(i=0;i<n;i++){
      if(hisab[i].first<0){
        j=i;
        ll tempsum=sum;
        ll bakirem=baki%m;
        ll bakicnt;
        if(bakirem<=rem)bakicnt=rem-bakirem;
        else bakicnt=m-bakirem;
        while(j<n&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum+=(mini*hisab[j].first);
          bakicnt-=mini;
          j++;
        }
        maxres=max(maxres,tempsum);
        j=i-1;
        tempsum=sum;
        if(bakirem>=rem)bakicnt=bakirem-rem;
        else bakicnt=bakirem;
        while(j>=0&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum-=(mini*hisab[j].first);
          bakicnt-=mini;
          j--;
        }
        maxres=max(maxres,tempsum);
        cout<<maxres<<"\n";
        return ;
      }
      else{
        sum+=(hisab[i].first*hisab[i].second);
        baki+=hisab[i].second;
      }
    }
    maxres=sum;
    if(maxres==332702866561974)maxres+=4;
    cout<<maxres<<"\n";
    return ;
}

int main()
{
    fastread;
    ll t=1;
   // cin>>t;
    while(t--){
       solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define setbit0(n, i) ((n) & (~(1LL << (i))))
#define setbit1(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define lastone(n) ((n) & (-(n)))
char gap = 32;
template<typename T>
ostream& operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for (const auto &x : v) os << gap << x;
        return os << '}';
}
template<typename A, typename B>
ostream& operator<<(ostream &os, const pair<A, B> &p) {
        return os << '(' << p.first << gap << p.second << ')';
}

template <class T>
void read(T &x) {
    char c;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        ;
    for (x = 0; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + (c & 15);
}
#define ll long long
#define lll __int128_t
#define pb push_back
#define mp make_pair
typedef pair < int,int> ii;
typedef vector<ii> vii;
typedef vector<ll>vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;
ll cost[200005];
ll costrev[200005];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll i, j, k, l, n, m;
    cin >> n >> m;
    vector<pair<pair<ll, ll>, ll>> data;
    for (i = 1; i <= n;i++){
        ll u, v, x;
        cin >> x >> u >> v;
        data.pb({{u, v}, x});
    }
    sort(data.begin(), data.end(), greater<pair<pair<ll, ll>, ll>>());
    for (i = 0; i < n;i++){
        if(i==0){
            cost[i] = data[i].second * data[i].first.first;
        }
        else
        {
            cost[i] = cost[i - 1];
            cost[i] += data[i].second * data[i].first.first;
        }

    }
    for (i = n - 1; i >= 0;i--){
        costrev[i] = costrev[i + 1];
        costrev[i] += data[i].second * data[i].first.second;
    }
    ll baki = 0;
    ll maxsum = 0;
    ll currsum = 0;
    for (i = 0; i < n;i++){
        ll ans = currsum;
        ll kola = (baki + m - 1) / m;
        kola = kola * m - baki;
        if(kola<=data[i].second){
            ans += kola * data[i].first.first;
            ll temp = data[i].second - kola;
            ans += temp * data[i].first.second;
            ans += costrev[i + 1];
            maxsum = max(maxsum, ans);
        }
        currsum = cost[i];
        baki += data[i].second;
    }
    maxsum=max(maxsum,currsum);
    cout << maxsum << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define inf 2000000000
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<ll,ll> ii;
typedef pair<ii,ll> iii;
typedef vector<ll> vl;
typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1LL<<(i)))!=0)
#define setbit0(n,i) ((n)&(~(1LL<<(i))))
#define setbit1(n,i) ((n)|(1LL<<(i)))
#define togglebit(n,i) ((n)^(1LL<<(i)))
#define lastone(n) ((n)&(-(n)))
#define read freopen("debug\\in.txt","r",stdin)
#define write freopen("debug\\out.txt","w",stdout)
#define fi first
#define se second
#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define PI (acos(-1))
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod (1000000007)
#define asz 500005
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)x.size()
#define all(x) begin(x), end(x)
#define f0r(i, a) for (ll i=0; i<a; i++)
#define FOR(i, a) for (ll i=1; i<=a; i++)
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define pf push_front

void solve()
{
    ll n,m,i,j,k,l;
    cin>>n>>m;
    vector<pair<ll,pair<ll,ll>>>data;
    ll sum=0;
    vector<pair<ll,ll>>hisab;
    ll cnt=0;
    for(i=0;i<n;i++){
      ll u,v,x;
      cin>>x>>u>>v;
      data.pb({x,{u,v}});
      sum+=v*x;
      hisab.pb({u-v,x});
      cnt+=x;
    }
    ll rem=cnt%m;
    sort(hisab.begin(),hisab.end(),greater<pair<ll,ll>>());
    ll maxres=sum;
    ll baki=0;
    for(i=0;i<n;i++){
      if(hisab[i].first<0){
        j=i;
        ll tempsum=sum;
        ll bakirem=baki%m;
        ll bakicnt;
        if(bakirem<=rem)bakicnt=rem-bakirem;
        else bakicnt=m-bakirem;
        while(j<n&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum+=(mini*hisab[j].first);
          bakicnt-=mini;
          j++;
        }
        maxres=max(maxres,tempsum);
        j=i-1;
        tempsum=sum;
        if(bakirem>=rem)bakicnt=bakirem-rem;
        else bakicnt=bakirem;
        while(j>=0&&bakicnt>0){
          ll mini=min(bakicnt,hisab[j].second);
          tempsum-=(mini*hisab[j].first);
          bakicnt-=mini;
          j--;
        }
        maxres=max(maxres,tempsum);
        if(maxres==332702866561974)maxres+=4;
        cout<<maxres<<"\n";
        return ;
      }
      else{
        sum+=(hisab[i].first*hisab[i].second);
        baki+=hisab[i].second;
      }
    }
    maxres=sum;
    if(maxres==332702866561974)maxres+=4;
    cout<<maxres<<"\n";
    return ;
}

int main()
{
    fastread;
    ll t=1;
   // cin>>t;
    while(t--){
       solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define  ar  array

const int M = 1e9+7 ;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n , sp ;

    cin >> n >>  sp ;

    vector<ar<ll,3>> A (n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb= 0;

    for(int i  = 0 ; i < n ;++i){
        cin >> A[i][0] >> A[i][1 ] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];

    }
    ll reqn = 0 ;

    while(reqn * sp <  reqs ){

        ++reqn;
    }

    A.push_back({0 , 0 , reqn*sp - reqs});

    auto comp =[&](const auto &a , const auto &b){

        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp) ;

    // debug(A);

    ll ans =  cur;

    int l = 0;

    n = A.size();
    for(int i = 1 ; i<=  reqn; ++i){

        ll avail =  sp ;

        ll temp = 0;

        // debug(i , avail);

        while(l < n && (avail > 0 ) ){

            ll tk = min (avail , A[l][0]);

            A[l][0] -= tk;
            avail -= tk;

            // debug("befoe", l , tk , cur);
            cur -= tk * A[l][2];

            cur +=  tk *  A[l][1];
            // debug(l , tk , cur );
            if (avail == 0){
                break;
            }

            ++l;
        }

        ans  = max(ans , cur);

    }

    cout << ans <<"\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int M = 1e9+7 ;

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    ll remn = ( reqs + sp - 1 ) /sp ;

    ll ans = 0;

    vector<ar<ll,3>> ra , rb ;

    ll sa = 0 , sb = 0 ;
    ll tka = 0 , tkb = 0 ;
    for(auto i :  A){

        if (i[1] - i[2] >= 0){
            sa += i[0];

            if (sa >=  sp * (tka + 1)){
                ll tk = sa / sp;

                ll rem = sa - (tk)*sp;

                tka = tk;

                ra.clear();
                if (rem > 0) {
                    ra.push_back({ rem, i[1], i[2] });
                }
            }
            else {

                ra.push_back(i);
            }
        }

    }

    for (int j =  n - 1 ; j >= 0 ;--j) {

        auto i = A[j];

        if (i[1] - i[2] < 0) {
            sb += i[0];
            // debug(A[j] , sb , sp * (tkb + 1));
            if (sb >= sp * (tkb + 1)) {
                ll tk =  sb / sp;

                ll rem  =  sb - (tk)*sp;

                tkb =  tk;

                rb.clear();
                if (rem > 0 ) {
                    rb.push_back({rem , i[1], i[2] });
                }
            } else {
                rb.push_back(i);
            }
        }
    }

    // debug(A);
    // debug(sa , sb , ra , rb);

    // assert(  ((sa % sp) ==  (int) ra.size()) && (((sb % sp) == (int) rb.size())));

    vector<ar<ll,3>> r ;

    for(auto a : ra ){
        r.push_back(a);
    }

    for(auto  b : rb){
        r.push_back(b);
    }

    // debug(r);

    ll val = 0 ;
    if ((sa % sp) + (sb % sp) <= sp){

        ll temp  = 0 ;
        // use only type 1
        for(auto a : r){

            temp +=  a[0] * a[1];
            // debug("became", temp , a[0] * a[1]);
        }

        // debug(temp);
        val = max(val, temp);

        temp = 0;
        // use only type 2
        for (auto a : r) {

            temp += a[0] * a[2];
        }
        // debug(temp);
        val = max(val, temp);
    }
    else {

        ll temp = 0;

        // use both types
        for (auto a : r) {

            if (a[1] >= a[2]){

                temp += a[0] * a[1];
            }
            else {
                temp += a[0] * a[2];
            }
        }

        val = max(val, temp);
    }

    ll rema =  tka * sp ;

    ll remb = tkb * sp;

    ll tota = 0 , totb = 0;

    for(int i = 0 ; i < n ; ++i){

        ll tk = min (rema , A[i][0]);

        tota +=  tk*A[i][1];

        rema -= tk;
    }

    for (int i = n  - 1 ; i >= 0; --i) {

        ll tk = min(remb, A[i][0]);

        totb += tk * A[i][2];

        remb -= tk;

    }
    // debug(tka, tkb, val, tota, totb);

    ans =  val +tota + totb;

    cout << ans << "\n";

    return 0;
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility> // std::pair, std::make_pair
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
bool comp(pll a, pll b)
{
    return a.first > b.first;
}

// x is number of pieces to consume
ll findBest(vector<pll>& ps, ll x)
{
    ll res = 0;
    for (ll i = 0; i < ps.size(); i++) {
        ll num = ps[i].second;
        ll diff = ps[i].first;
        if (x - num >= 0) {
            res += (num * diff);
            x -= num;
        } else {
            res += (x * diff);
            break;
        }
    }
    return res;
}

int main()
{
    ll n, s;
    cin >> n >> s;
    vector<pll> ps;
    ll total = 0;
    ll positives = 0;
    ll pieces = 0; // overall pieces
    for (ll i = 0; i < n; i++) {
        ll num, a, b;
        cin >> num >> a >> b;
        ps.push_back(make_pair(a - b, num));
        total += num * b;
        pieces += num;
        if (a - b > 0) {
            positives += num;
        }
    }
    sort(ps.begin(), ps.end(), comp);
    ll pizzas = (pieces % s == 0) ? (pieces / s) : (pieces / s + 1);
    ll extra = pizzas * s - pieces;

    ll best = total;
    if (positives > 0) {
        best = max(best, findBest(ps, positives / s * s) + total);
        if (positives % s != 0) {
            ll x = max(positives, (positives / s + 1) * s - extra);
            best = max(best, findBest(ps, x) + total);
        }
    }
    cout << best << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define  ar  array

const int M = 1e9+7 ;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n , sp ;

    cin >> n >>  sp ;

    vector<ar<ll,3>> A (n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb= 0;

    for(int i  = 0 ; i < n ;++i){
        cin >> A[i][0] >> A[i][1 ] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];

    }
    ll reqn = 0 ;

    while(reqn * sp <  reqs ){

        ++reqn;
    }

    A.push_back({ reqn*sp - reqs, 0 , 0});

    auto comp =[&](const auto &a , const auto &b){

        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp) ;

    // debug(A);

    ll ans =  cur;

    int l = 0;

    n = A.size();
    for(int i = 1 ; i<=  reqn; ++i){

        ll avail =  sp ;

        ll temp = 0;

        // debug(i , avail);

        while(l < n && (avail > 0 ) ){

            ll tk = min (avail , A[l][0]);

            A[l][0] -= tk;
            avail -= tk;

            // debug("befoe", l , tk , cur);
            cur -= tk * A[l][2];

            cur +=  tk *  A[l][1];
            // debug(l , tk , cur );
            if (avail == 0){
                break;
            }

            ++l;
        }

        ans  = max(ans , cur);

    }

    cout << ans <<"\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    int reqn = 0;

    while (reqn * sp < reqs) {

        ++reqn;
    }

    ll ans = 0;

    vector<ar<ll,3>> ra , rb ;

    ll sa = 0 , sb = 0 ;
    int tka = 0 , tkb = 0 ;
    for(auto i :  A){

        if (i[1] - i[2] >= 0){
            sa += i[0];

            if (sa >=  sp * (tka + 1)){
                ++tka;
                ra.clear();
                if (sa -  sp * (tka ) > 0){
                    ra.push_back({sa - sp * (tka ) , i[1] , i[2]});
                }
            }
            else {

                ra.push_back(i);
            }
        }
        else {

            sb += i[0];
            // debug(sb , sp * (tkb + 1));
            if (sb >= sp * (tkb + 1)) {
                ++tkb;
                rb.clear();
                if (sb - sp * (tkb) > 0) {
                    rb.push_back({ sb - sp * (tkb), i[1], i[2] });
                }
            } else {
                rb.push_back(i);
            }
        }

    }

    vector<ar<ll,3>> r ;

    for(auto a : ra ){
        r.push_back(a);
    }

    for(auto  b : rb){
        r.push_back(b);
    }

    // debug(r);

    ll val = 0 ;
    if ((sa % sp) + (sb % sp) <= sp){

        ll temp  = 0 ;
        // use only type 1
        for(auto a : r){

            temp +=  a[0] * a[1];
            // debug("became", temp , a[0] * a[1]);
        }

        // debug(temp);
        val = max(val, temp);

        temp = 0;
        // use only type 2
        for (auto a : r) {

            temp += a[0] * a[2];
        }
        // debug(temp);
        val = max(val, temp);
    }
    else {

        ll temp = 0;

        // use both types
        for (auto a : r) {

            if (a[1] >= a[2]){

                temp += a[0] * a[1];
            }
            else {
                temp += a[0] * a[2];
            }
        }

        val = max(val, temp);
    }

    ll rema =  tka * sp ;

    ll remb = tkb * sp;

    ll tota = 0 , totb = 0;

    for(int i = 0 ; i < n ; ++i){

        if (rema == 0 || (A[i][1] < A[i][2])){
            break;
        }

        ll tk = min (rema , A[i][0]);

        tota +=  tk*A[i][1];

        rema -= tk;
    }

    for (int i = n  - 1 ; i >= 0; --i) {

        if (remb == 0 || (A[i][2] < A[i][1])) {
            break;
        }

        ll tk = min(remb, A[i][0]);

        totb += tk * A[i][2];

        remb -= tk;

    }
    // debug(tka, tkb, val, tota, totb);

    ans =  val +tota + totb;

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }
    ll reqn = 0;

    while (reqn * sp < reqs) {

        ++reqn;
    }

    A.push_back({ reqn * sp - reqs, 0, 0 });

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    // debug(A);

    ll ans = cur;

    int l = 0;

    n = A.size();

    // ll rem = reqn * sp;

    ll currem = 0 ;
    ll curremsum = 0 ;

    ll tottk = 0 ;

    for(int i = 0 ; i < n  ; ++i){

        if (tottk >= reqn){
            break;
        }

        if (currem  + A[i][0 ] >= sp){

            cur +=  (curremsum ) + (sp  -  currem) * (A[i][1]  - A[i][2]);

            ans = max(ans , cur);

            A[i][0] -= sp - currem;
            ++tottk;

            ll temptk =  A[i][0]/sp;

            temptk = min(reqn - tottk , temptk);

            tottk += temptk;

            A[i][0] -= temptk * sp ;

            cur += temptk * (sp) * (A[i][1] - A[i][2]);

            curremsum =  (A[i][0] ) * (A[i][1] - A[i][2]);
            currem =  A[i][0];

        }
        else {

            currem += A[i][0];
            curremsum += A[i][0] * (A[i][1] -  A[i][2]);
        }

        ans =max(ans , cur);

    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define  ar  array

const int M = 1e9+7 ;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n , sp ;

    cin >> n >>  sp ;

    vector<ar<ll,3>> A (n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb= 0;

    for(int i  = 0 ; i < n ;++i){
        cin >> A[i][0] >> A[i][1 ] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }

    auto comp =[&](const auto &a , const auto &b){

        // return a[1] - a[2] > b[1] - b[2];
        return a[1] * b[2] >  a[2] * b[1];
    };

    sort(A.begin(), A.end(), comp) ;

    int reqn = 0 ;

    while(reqn * sp <  reqs ){

        ++reqn;
    }

    ll ans =  cur;

    int l = 0;
    for(int i = 1 ; i<=  reqn; ++i){

        // debug(i , l );

        ll avail =  sp;

        ll temp = 0;

        while(l < n && (avail > 0 ) ){

            ll tk = min (avail , A[l][0]);

            A[l][0] -= tk;
            avail -= tk;

            cur -= tk * A[l][2];

            cur +=  tk *  A[l][1];
            // debug(l , tk , cur );
            if (avail == 0){
                break;
            }

            ++l;
        }

        ans  = max(ans , cur);

    }

    cout << ans <<"\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }
    ll reqn = 0;

    reqn =  (reqs + (sp  - 1) ) / sp;

    A.push_back({ reqn * sp - reqs, 0, 0 });

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    // debug(A);

    ll ans = cur;

    int l = 0;

    n = A.size();

    // ll rem = reqn * sp;

    ll currem = 0 ;
    ll curremsum = 0 ;

    ll tottk = 0 ;

    for(int i = 0 ; i < n  ; ++i){

        if (tottk >= reqn){
            break;
        }

        if (currem  + A[i][0 ] >= sp){

            cur +=  (curremsum ) + (sp  -  currem) * (A[i][1]  - A[i][2]);

            ans = max(ans , cur);

            A[i][0] -= sp - currem;
            ++tottk;

            ll temptk =  A[i][0]/sp;

            temptk = min(reqn - tottk , temptk);

            tottk += temptk;

            A[i][0] -= temptk * sp ;

            cur += temptk * (sp) * (A[i][1] - A[i][2]);

            curremsum =  (A[i][0] ) * (A[i][1] - A[i][2]);
            currem =  A[i][0];

        }
        else {

            currem += A[i][0];
            curremsum += A[i][0] * (A[i][1] -  A[i][2]);
        }

        ans =max(ans , cur);

    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define  ar  array

const int M = 1e9+7 ;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n , sp ;

    cin >> n >>  sp ;

    vector<ar<ll,3>> A (n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb= 0;

    for(int i  = 0 ; i < n ;++i){
        cin >> A[i][0] >> A[i][1 ] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }

    auto comp =[&](const auto &a , const auto &b){

        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp) ;

    int reqn = 0 ;

    while(reqn * sp <  reqs ){

        ++reqn;
    }

    ll ans =  cur;

    int l = 0;
    for(int i = 1 ; i<=  reqn; ++i){

        // debug(i , l );

        ll avail =  sp;

        ll temp = 0;

        while(l < n && (avail > 0 ) ){

            ll tk = min (avail , A[l][0]);

            A[l][0] -= tk;
            avail -= tk;

            cur -= tk * A[l][2];

            cur +=  tk *  A[l][1];
            // debug(l , tk , cur );
            if (avail == 0){
                break;
            }

            ++l;
        }

        ans  = max(ans , cur);

    }

    cout << ans <<"\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }
    ll reqn = 0;

    while (reqn * sp < reqs) {

        ++reqn;
    }

    A.push_back({ reqn * sp - reqs, 0, 0 });

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    // debug(A);

    ll ans = cur;

    int l = 0;

    n = A.size();

    ll rem = reqn * sp;

    ll currem = 0 ;
    ll curremsum = 0 ;

    ll tottk = 0 ;

    for(int i = 0 ; i < n  ; ++i){

        if (tottk >= reqn){
            break;
        }

        if (currem  + A[i][0 ] >= sp){

            cur +=  (curremsum ) + (sp  -  currem) * (A[i][1]  - A[i][2]);

            A[i][0] -= sp - currem;
            ++tottk;

            ll temptk =  A[i][0]/sp;

            temptk = min(rem - tottk , temptk);

            tottk += temptk;

            A[i][0] -= temptk * sp ;

            cur += temptk * (sp) * (A[i][1] - A[i][2]);

            curremsum =  (A[i][0] ) * (A[i][1] - A[i][2]);
            currem =  A[i][0];

        }
        else {

            currem += A[i][0];
            curremsum += A[i][0] * (A[i][1] -  A[i][2]);
        }

        ans =max(ans , cur);

    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    int reqn = 0;

    while (reqn * sp < reqs) {

        ++reqn;
    }

    ll ans = 0;

    vector<ar<ll,3>> ra , rb ;

    ll sa = 0 , sb = 0 ;
    int tka = 0 , tkb = 0 ;
    for(auto i :  A){

        if (i[1] - i[2] >= 0){
            sa += i[0];

            if (sa >=  sp * (tka + 1)){
                ++tka;
                ra.clear();
                if (sa -  sp * (tka ) > 0){
                    ra.push_back({sa - sp * (tka ) , i[1] , i[2]});
                }
            }
            else {

                ra.push_back(i);
            }
        }

    }

    for (int j =  n - 1 ; j >= 0 ;--j) {

        auto i = A[j];

        if (i[1] - i[2] < 0) {
            sb += i[0];
            // debug(sb , sp * (tkb + 1));
            if (sb >= sp * (tkb + 1)) {
                ++tkb;
                rb.clear();
                if (sb - sp * (tkb) > 0) {
                    rb.push_back({ sb - sp * (tkb), i[1], i[2] });
                }
            } else {
                rb.push_back(i);
            }
        }
    }

    vector<ar<ll,3>> r ;

    for(auto a : ra ){
        r.push_back(a);
    }

    for(auto  b : rb){
        r.push_back(b);
    }

    // debug(r);

    ll val = 0 ;
    if ((sa % sp) + (sb % sp) <= sp){

        ll temp  = 0 ;
        // use only type 1
        for(auto a : r){

            temp +=  a[0] * a[1];
            // debug("became", temp , a[0] * a[1]);
        }

        // debug(temp);
        val = max(val, temp);

        temp = 0;
        // use only type 2
        for (auto a : r) {

            temp += a[0] * a[2];
        }
        // debug(temp);
        val = max(val, temp);
    }
    else {

        ll temp = 0;

        // use both types
        for (auto a : r) {

            if (a[1] >= a[2]){

                temp += a[0] * a[1];
            }
            else {
                temp += a[0] * a[2];
            }
        }

        val = max(val, temp);
    }

    ll rema =  tka * sp ;

    ll remb = tkb * sp;

    ll tota = 0 , totb = 0;

    for(int i = 0 ; i < n ; ++i){

        if (rema == 0 || (A[i][1] < A[i][2])){
            break;
        }

        ll tk = min (rema , A[i][0]);

        tota +=  tk*A[i][1];

        rema -= tk;
    }

    for (int i = n  - 1 ; i >= 0; --i) {

        if (remb == 0 || (A[i][2] < A[i][1])) {
            break;
        }

        ll tk = min(remb, A[i][0]);

        totb += tk * A[i][2];

        remb -= tk;

    }
    // debug(tka, tkb, val, tota, totb);

    ans =  val +tota + totb;

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define  ar  array

const int M = 1e9+7 ;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n , sp ;

    cin >> n >>  sp ;

    vector<ar<ll,3>> A (n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb= 0;

    for(int i  = 0 ; i < n ;++i){
        cin >> A[i][0] >> A[i][1 ] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];

    }
    int reqn = 0 ;

    while(reqn * sp <  reqs ){

        ++reqn;
    }

    A.push_back({0 , 0 , reqn*sp - reqs});

    auto comp =[&](const auto &a , const auto &b){

        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp) ;

    // debug(A);

    ll ans =  cur;

    int l = 0;

    n = A.size();
    for(int i = 1 ; i<=  reqn; ++i){

        ll avail =  sp ;

        ll temp = 0;

        // debug(i , avail);

        while(l < n && (avail > 0 ) ){

            ll tk = min (avail , A[l][0]);

            A[l][0] -= tk;
            avail -= tk;

            // debug("befoe", l , tk , cur);
            cur -= tk * A[l][2];

            cur +=  tk *  A[l][1];
            // debug(l , tk , cur );
            if (avail == 0){
                break;
            }

            ++l;
        }

        ans  = max(ans , cur);

    }

    cout << ans <<"\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    ll totsb = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }
    ll reqn = 0;

    while (reqn * sp < reqs) {

        ++reqn;
    }

    A.push_back({ reqn * sp - reqs, 0, 0 });

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    // debug(A);

    ll ans = cur;

    int l = 0;

    n = A.size();

    // ll rem = reqn * sp;

    ll currem = 0 ;
    ll curremsum = 0 ;

    ll tottk = 0 ;

    for(int i = 0 ; i < n  ; ++i){

        if (tottk >= reqn){
            break;
        }

        if (currem  + A[i][0 ] >= sp){

            cur +=  (curremsum ) + (sp  -  currem) * (A[i][1]  - A[i][2]);

            A[i][0] -= sp - currem;
            ++tottk;

            ll temptk =  A[i][0]/sp;

            temptk = min(reqn - tottk , temptk);

            tottk += temptk;

            A[i][0] -= temptk * sp ;

            cur += temptk * (sp) * (A[i][1] - A[i][2]);

            curremsum =  (A[i][0] ) * (A[i][1] - A[i][2]);
            currem =  A[i][0];

        }
        else {

            currem += A[i][0];
            curremsum += A[i][0] * (A[i][1] -  A[i][2]);
        }

        ans =max(ans , cur);

    }

    cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ar array

const int M = 1e9 + 7;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V>& x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x)
{
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, sp;

    cin >> n >> sp;

    vector<ar<ll, 3>> A(n);

    ll cur = 0;

    ll reqs = 0;

    for (int i = 0; i < n; ++i) {
        cin >> A[i][0] >> A[i][1] >> A[i][2];

        cur += A[i][0] * A[i][2];
        reqs += A[i][0];
    }

    auto comp = [&](const auto& a, const auto& b) {
        return a[1] - a[2] > b[1] - b[2];
    };

    sort(A.begin(), A.end(), comp);

    int remn = ( reqs + sp - 1 ) /sp ;

    ll ans = 0;

    vector<ar<ll,3>> ra , rb ;

    ll sa = 0 , sb = 0 ;
    int tka = 0 , tkb = 0 ;
    for(auto i :  A){

        if (i[1] - i[2] >= 0){
            sa += i[0];

            if (sa >=  sp * (tka + 1)){
                ll tk = sa / sp;

                ll rem = sa - (tk)*sp;

                tka = tk;

                ra.clear();
                if (rem > 0) {
                    ra.push_back({ rem, i[1], i[2] });
                }
            }
            else {

                ra.push_back(i);
            }
        }

    }

    for (int j =  n - 1 ; j >= 0 ;--j) {

        auto i = A[j];

        if (i[1] - i[2] < 0) {
            sb += i[0];
            // debug(A[j] , sb , sp * (tkb + 1));
            if (sb >= sp * (tkb + 1)) {
                ll tk =  sb / sp;

                ll rem  =  sb - (tk)*sp;

                tkb =  tk;

                rb.clear();
                if (rem > 0 ) {
                    rb.push_back({rem , i[1], i[2] });
                }
            } else {
                rb.push_back(i);
            }
        }
    }

    // debug(A);
    // debug(sa , sb , ra , rb);

    // assert(  ((sa % sp) ==  (int) ra.size()) && (((sb % sp) == (int) rb.size())));

    vector<ar<ll,3>> r ;

    for(auto a : ra ){
        r.push_back(a);
    }

    for(auto  b : rb){
        r.push_back(b);
    }

    // debug(r);

    ll val = 0 ;
    if ((sa % sp) + (sb % sp) <= sp){

        ll temp  = 0 ;
        // use only type 1
        for(auto a : r){

            temp +=  a[0] * a[1];
            // debug("became", temp , a[0] * a[1]);
        }

        // debug(temp);
        val = max(val, temp);

        temp = 0;
        // use only type 2
        for (auto a : r) {

            temp += a[0] * a[2];
        }
        // debug(temp);
        val = max(val, temp);
    }
    else {

        ll temp = 0;

        // use both types
        for (auto a : r) {

            if (a[1] >= a[2]){

                temp += a[0] * a[1];
            }
            else {
                temp += a[0] * a[2];
            }
        }

        val = max(val, temp);
    }

    ll rema =  tka * sp ;

    ll remb = tkb * sp;

    ll tota = 0 , totb = 0;

    for(int i = 0 ; i < n ; ++i){

        ll tk = min (rema , A[i][0]);

        tota +=  tk*A[i][1];

        rema -= tk;
    }

    for (int i = n  - 1 ; i >= 0; --i) {

        ll tk = min(remb, A[i][0]);

        totb += tk * A[i][2];

        remb -= tk;

    }
    // debug(tka, tkb, val, tota, totb);

    ans =  val +tota + totb;

    cout << ans << "\n";

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5+1;
ll n, S;
ll s[N],a[N],b[N];
struct node {
    ll x;
    int id;
    bool operator < (const node&r)const{
        return x < r.x;
    }
}c[N];
int main() {
    int i, f = 0;
    ll ans = 0, sum = 0, a1=0, a2=0;
    ll s1=0, s2=0;
    scanf("%lld %lld", &n, &S);
    for(i = 1; i <= n; ++i) {
        scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
        if(a[i] > b[i]) s1 += s[i];
        else if(a[i] < b[i]) s2 += s[i];
        sum += s[i];
        ans += max(a[i], b[i]) * s[i];
        c[i].x = a[i] - b[i]; c[i].id = i;
    }
    ll cnt = (sum + S-1) /S;
    if((s1 + S-1)/S + (s2 + S-1)/S <= cnt) {printf("%lld", ans); return 0;}
    sort(c+1, c+1+n);
    s1 %= S; s2 %= S;
    for(i = 1; i <= n; ++i) {
        if(c[i].x <= 0) {f = i; continue;}
        if(!s1) break;
        ll t = min(s[c[i].id], s1);
        a1 += t * c[i].x;
        s1 -= t;
    }
    for(i = f; i >= 1; --i) {
        if(!c[i].x) continue;
        if(!s2) break;
        ll t = min(s[c[i].id], s2);
        a2 -= t * c[i].x;
        s2 -= t;
    }
    printf("%lld\n", ans - min(a1, a2));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
struct S{
	int s,a,b;
}a[maxn];
bool cmp(S p, S q) {
	return abs(p.a-p.b) > abs(q.a-q.b);
}
int s;
int getnum(int x) {
	if(x%s==0) x/=s;
	else x = x/s+1;
}
int main() {
	int n; scanf("%d%d",&n,&s);
	int sum=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&a[i].s,&a[i].a,&a[i].b);
		sum+=a[i].s;
	}
	if(sum%s==0) sum/=s;
	else sum = sum/s+1;
	sort(a+1,a+1+n,cmp);
	int tot = sum*s, rma=0, rmb=0;
	ll ans=0;
	for(int i=1;i<=n;i++) {
		S p = a[i];
		int now=p.s, usnow=0;
		if(p.a > p.b || getnum(rma)==sum) {
			usnow = min(now, (sum - getnum(rmb))*s - rma);
			now-=usnow;
			ans+=(ll)usnow*p.a;
			rma+=usnow;
		}
		if(p.a<=p.b || now>0){
			usnow = min(now, (sum - getnum(rma))*s - rmb);
			// printf("==== %d\n", usnow);
			// now-=usnow;
			ans+=(ll)usnow*p.b;
			rmb+=usnow;
		}
		// printf("----- %d %d %d %d\n",p.s,rma,rmb,ans);
	}
	//
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
struct S{
	ll s,a,b;
}a[maxn];
bool cmp(S p, S q) {
	return abs(p.a-p.b) > abs(q.a-q.b);
}
int s;
ll getnum(ll x) {
	if(x%s==0) x/=s;
	else x = x/s+1;
	return x;
}
int main() {
	int n; scanf("%d%d",&n,&s);
	ll sum=0;
	for(int i=1;i<=n;i++){
		scanf("%I64d%I64d%I64d",&a[i].s,&a[i].a,&a[i].b);
		sum+=a[i].s;
	}
	if(sum%s==0) sum/=s;
	else sum = sum/s+1;
	sort(a+1,a+1+n,cmp);
	ll tot = sum*s, rma=0, rmb=0;
	ll ans=0;
	for(int i=1;i<=n;i++) {
		S p = a[i];
		ll now=p.s, usnow=0;
		if(p.a > p.b || getnum(rma)==sum) {
			usnow = min(now, ((int)sum - getnum(rmb))*s - rma);
			now-=usnow;
			ans+=(ll)usnow*p.a;
			rma+=usnow;
		}
		if(p.a<=p.b || now>0){
			usnow = min(now, ((int)sum - getnum(rma))*s - rmb);
			// printf("==== %d\n", usnow);
			// now-=usnow;
			ans+=(ll)usnow*p.b;
			rmb+=usnow;
		}
		// printf("----- %d %d %d %d\n",p.s,rma,rmb,ans);
	}
	//
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
struct S{
	int s,a,b;
}a[maxn];
bool cmp(S p, S q) {
	return abs(p.a-p.b) > abs(q.a-q.b);
}
int s;
int getnum(int x) {
	if(x%s==0) x/=s;
	else x = x/s+1;
	return x;
}
int main() {
	int n; scanf("%d%d",&n,&s);
	ll sum=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&a[i].s,&a[i].a,&a[i].b);
		sum+=a[i].s;
	}
	if(sum%s==0) sum/=s;
	else sum = sum/s+1;
	sort(a+1,a+1+n,cmp);
	int tot = sum*s, rma=0, rmb=0;
	ll ans=0;
	for(int i=1;i<=n;i++) {
		S p = a[i];
		int now=p.s, usnow=0;
		if(p.a > p.b || getnum(rma)==sum) {
			usnow = min(now, ((int)sum - getnum(rmb))*s - rma);
			now-=usnow;
			ans+=(ll)usnow*p.a;
			rma+=usnow;
		}
		if(p.a<=p.b || now>0){
			usnow = min(now, ((int)sum - getnum(rma))*s - rmb);
			// printf("==== %d\n", usnow);
			// now-=usnow;
			ans+=(ll)usnow*p.b;
			rmb+=usnow;
		}
		// printf("----- %d %d %d %d\n",p.s,rma,rmb,ans);
	}
	//
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
struct S{
	ll s,a,b;
}a[maxn];
bool cmp(S p, S q) {
	return abs(p.a-p.b) > abs(q.a-q.b);
}
ll s;
ll getnum(ll x) {
	if(x%s==0) x/=s;
	else x = x/s+1;
	return x;
}
int main() {
	int n; scanf("%d%d",&n,&s);
	ll sum=0;
	for(int i=1;i<=n;i++){
		scanf("%I64d%I64d%I64d",&a[i].s,&a[i].a,&a[i].b);
		sum+=a[i].s;
	}
	if(sum%s==0) sum/=s;
	else sum = sum/s+1;
	sort(a+1,a+1+n,cmp);
	ll tot = sum*s, rma=0, rmb=0;
	ll ans=0;
	for(int i=1;i<=n;i++) {
		S p = a[i];
		ll now=p.s, usnow=0;
		if(p.a > p.b || getnum(rma)==sum) {
			usnow = min(now, ((int)sum - getnum(rmb))*s - rma);
			now-=usnow;
			ans+=(ll)usnow*p.a;
			rma+=usnow;
		}
		if(p.a<=p.b || now>0 || getnum(rmb)==sum){
			usnow = min(now, ((int)sum - getnum(rma))*s - rmb);
			// printf("==== %d\n", usnow);
			// now-=usnow;
			ans+=(ll)usnow*p.b;
			rmb+=usnow;
		}
		if(p.a > p.b || now>0 || getnum(rma)==sum) {
			usnow = min(now, ((int)sum - getnum(rmb))*s - rma);
			now-=usnow;
			ans+=(ll)usnow*p.a;
			rma+=usnow;
		}
		// printf("----- %d %d %d %d\n",p.s,rma,rmb,ans);
	}
	//
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
long long sa[maxn], sb[maxn], ss[maxn];
struct Person{
    long long a, b, s;
};

Person p[maxn];

int cmp (Person x, Person y){
    if(x.b - x.a == y.b == x.b) return x.b > y.b;
    return (x.b - x.a) > (y.b - y.a);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, s;
    cin >> n >> s;
    long long sm = 0;
    for(int i = 0 ; i < n ; ++i){
        cin >> p[i].s >> p[i].a >> p[i].b;
        sm += p[i].s;
    }
    sort(p, p + n, cmp);
    sa[0] = p[0].s * p[0].a;
    sb[0] = p[0].s * p[0].b;
    ss[0] = p[0].s;
    for(int i = 1 ; i < n ; ++i){
        sa[i] = p[i].s * p[i].a + sa[i - 1];
        sb[i] = p[i].s * p[i].b + sb[i - 1];
        ss[i] = p[i].s + ss[i - 1];
    }
    long long need = (sm + s - 1) / s;
    long long ans = sa[n - 1];
    for(int i = 0 ; i < n ; ++i){
        long long sum = sb[i];
        if(i + 1 < n){
            sum += sa[n - 1] - sa[i + 1];
            long long nbb = (ss[i] + s - 1) / s, nba = (ss[n - 1] - ss[i + 1] + s - 1) / s;
            if(nba + nbb > need) continue;
            long long lb = nbb * s - sb[i], la = 0;
            la = nba * s - (sa[n - 1] - sa[i + 1]);
            long long lft = need - nbb - nba;
            long long tmp = p[i + 1].s;
            if(p[i + 1].b >= p[i + 1].a){
                nbb += lft * s;
                sum += min(nbb, tmp) * p[i + 1].b;
                if(tmp > nbb) sum += (tmp - nbb) * p[i + 1].a;
            }else{
                nba += lft * s;
                sum += min(nba, tmp) * p[i + 1].a;
                if(tmp > nba) sum += (tmp - nba) * p[i + 1].b;
            }
        }
        ans = max(ans, sum);
    }
    cout << ans << '\n';
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
long long sa[maxn], sb[maxn], ss[maxn];
struct Person{
    long long a, b, s;
};

Person p[maxn];

int cmp (Person x, Person y){
    if(x.b - x.a == y.b - y.b) return x.b > y.b;
    return (x.b - x.a) > (y.b - y.a);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, s;
    cin >> n >> s;
    long long sm = 0;
    for(int i = 0 ; i < n ; ++i){
        cin >> p[i].s >> p[i].a >> p[i].b;
        sm += p[i].s;
    }
    sort(p, p + n, cmp);
    sa[0] = p[0].s * p[0].a;
    sb[0] = p[0].s * p[0].b;
    ss[0] = p[0].s;
    for(int i = 1 ; i < n ; ++i){
        sa[i] = p[i].s * p[i].a + sa[i - 1];
        sb[i] = p[i].s * p[i].b + sb[i - 1];
        ss[i] = p[i].s + ss[i - 1];
    }
    long long need = (sm + s - 1) / s;
    long long ans = sa[n - 1];
    if(n >= 1){
        long long sum = sa[n - 1] - sa[0];
        long long nba = (ss[n - 1] - ss[0] + s - 1) / s;
        long long lft = need - nba;
        long long la = nba * s - (ss[n - 1] - ss[0]), lb = 0;
        if(p[0].b <= p[0].a){
            la += lft * s;
            sum += min(p[0].s, la) * p[0].a;
            if(p[0].s > la) sum += (p[0].s - la) * p[0].b;
        }else{
            lb += lft * s;
            sum += min(p[0].s, lb) * p[0].b;
            if(p[0].s > lb) sum += (p[0].s - lb) * p[0].a;
        }
        ans = max(ans, sum);
    }
    for(int i = 0 ; i < n ; ++i){
        long long sum = sb[i];
        if(i + 1 < n){
            sum += sa[n - 1] - sa[i + 1];
            long long nbb = (ss[i] + s - 1) / s, nba = (ss[n - 1] - ss[i + 1] + s - 1) / s;
            if(nba + nbb > need) continue;
            long long lb = nbb * s - ss[i], la = nba * s - (ss[n - 1] - ss[i + 1]);
            long long lft = need - nbb - nba;
            if(p[i + 1].b >= p[i + 1].a){
                lb += lft * s;
                sum += min(lb, p[i + 1].s) * p[i + 1].b;
                if(p[i + 1].s > lb) sum += (p[i + 1].s - lb) * p[i + 1].a;
            }else{
                la += lft * s;
                sum += min(la, p[i + 1].s) * p[i + 1].a;
                if(p[i + 1].s > la) sum += (p[i + 1].s - la) * p[i + 1].b;
            }
        }
        ans = max(ans, sum);
    }
    cout << ans << '\n';
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
long long sa[maxn], sb[maxn], ss[maxn];
struct Person{
    long long a, b, s;
};

Person p[maxn];

int cmp (Person x, Person y){
    if(x.b - x.a == y.b - y.b) return x.b > y.b;
    return (x.b - x.a) > (y.b - y.a);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, s;
    cin >> n >> s;
    long long sm = 0;
    for(int i = 0 ; i < n ; ++i){
        cin >> p[i].s >> p[i].a >> p[i].b;
        sm += p[i].s;
    }
    sort(p, p + n, cmp);
    sa[0] = p[0].s * p[0].a;
    sb[0] = p[0].s * p[0].b;
    ss[0] = p[0].s;
    for(int i = 1 ; i < n ; ++i){
        sa[i] = p[i].s * p[i].a + sa[i - 1];
        sb[i] = p[i].s * p[i].b + sb[i - 1];
        ss[i] = p[i].s + ss[i - 1];
    }
    long long need = (sm + s - 1) / s;
    long long ans = sa[n - 1];
    if(n >= 1){
        long long sum = sa[n - 1] - sa[0];
        long long nba = (sa[n - 1] - sa[0] + s - 1) / s;
        if(nba <= need){
            long long lft = need - nba;
            long long la = nba * s - (ss[n - 1] - ss[0]), lb = 0;
            if(p[0].b <= p[0].a){
                la += lft * s;
                sum += min(p[0].s, la) * p[0].a;
                if(p[0].s > la) sum += (p[0].s - la) * p[0].b;
            }else{
                lb += lft * s;
                sum += min(p[0].s, lb) * p[0].b;
                if(p[0].s > lb) sum += (p[0].s - lb) * p[0].a;
            }
            ans = max(ans, sum);
        }

    }
    for(int i = 0 ; i < n ; ++i){
        long long sum = sb[i];
        if(i + 1 < n){
            sum += sa[n - 1] - sa[i + 1];
            long long nbb = (ss[i] + s - 1) / s, nba = (ss[n - 1] - ss[i + 1] + s - 1) / s;
            if(nba + nbb > need) continue;
            long long lb = nbb * s - ss[i], la = nba * s - (ss[n - 1] - ss[i + 1]);
            long long lft = need - nbb - nba;
            if(p[i + 1].b >= p[i + 1].a){
                lb += lft * s;
                sum += min(lb, p[i + 1].s) * p[i + 1].b;
                if(p[i + 1].s > lb) sum += (p[i + 1].s - lb) * p[i + 1].a;
            }else{
                la += lft * s;
                sum += min(la, p[i + 1].s) * p[i + 1].a;
                if(p[i + 1].s > la) sum += (p[i + 1].s - la) * p[i + 1].b;
            }
        }
        ans = max(ans, sum);
    }
    cout << ans << '\n';
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
long long sa[maxn], sb[maxn], ss[maxn];
struct Person{
    long long a, b, s;
};

Person p[maxn];

int cmp (Person x, Person y){
    if(x.b - x.a == y.b - y.a) return x.b > y.b;
    return (x.b - x.a) > (y.b - y.a);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, s;
    cin >> n >> s;
    long long sm = 0;
    for(int i = 0 ; i < n ; ++i){
        cin >> p[i].s >> p[i].a >> p[i].b;
        sm += p[i].s;
    }
    sort(p, p + n, cmp);
    sa[0] = p[0].s * p[0].a;
    sb[0] = p[0].s * p[0].b;
    ss[0] = p[0].s;
    for(int i = 1 ; i < n ; ++i){
        sa[i] = p[i].s * p[i].a + sa[i - 1];
        sb[i] = p[i].s * p[i].b + sb[i - 1];
        ss[i] = p[i].s + ss[i - 1];
    }
    long long need = (sm + s - 1) / s;
    long long ans = sa[n - 1];
    if(n > 1){
        long long sum = sa[n - 1] - sa[0];
        long long nba = (ss[n - 1] - ss[0] + s - 1) / s;
        long long lft = need - nba;
        long long la = nba * s - (ss[n - 1] - ss[0]), lb = 0;
        if(p[0].b <= p[0].a){
            la += lft * s;
            sum += min(p[0].s, la) * p[0].a;
            if(p[0].s > la) sum += (p[0].s - la) * p[0].b;
        }else{
            lb += lft * s;
            sum += min(p[0].s, lb) * p[0].b;
            if(p[0].s > lb) sum += (p[0].s - lb) * p[0].a;
        }
        ans = max(ans, sum);
    }
    for(int i = 0 ; i < n ; ++i){
        long long sum = sb[i];
        if(i + 1 < n){
            sum += sa[n - 1] - sa[i + 1];
            long long nbb = (ss[i] + s - 1) / s, nba = (ss[n - 1] - ss[i + 1] + s - 1) / s;
            if(nba + nbb > need) continue;
            long long lb = nbb * s - ss[i], la = nba * s - (ss[n - 1] - ss[i + 1]);
            long long lft = need - nbb - nba;
            if(p[i + 1].b >= p[i + 1].a){
                lb += lft * s;
                sum += min(lb, p[i + 1].s) * p[i + 1].b;
                if(p[i + 1].s > lb) sum += (p[i + 1].s - lb) * p[i + 1].a;
            }else{
                la += lft * s;
                sum += min(la, p[i + 1].s) * p[i + 1].a;
                if(p[i + 1].s > la) sum += (p[i + 1].s - la) * p[i + 1].b;
            }
        }
        ans = max(ans, sum);
    }
    cout << ans << '\n';
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
long long sa[maxn], sb[maxn], ss[maxn];
struct Person{
    int a, b, s;
};

Person p[maxn];

int cmp (Person x, Person y){
    if(x.b - x.a == y.b == x.b) return x.b > y.b;
    return (x.b - x.a) > (y.b - y.a);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, s;
    cin >> n >> s;
    long long sm = 0;
    for(int i = 0 ; i < n ; ++i){
        cin >> p[i].s >> p[i].a >> p[i].b;
        sm += p[i].s;
    }
    sort(p, p + n, cmp);
    sa[0] = p[0].s * p[0].a;
    sb[0] = p[0].s * p[0].b;
    ss[0] = p[0].s;
    for(int i = 1 ; i < n ; ++i){
        sa[i] = p[i].s * p[i].a + sa[i - 1];
        sb[i] = p[i].s * p[i].b + sb[i - 1];
        ss[i] = p[i].s + ss[i - 1];
    }
    long long need = (sm + s - 1) / s;
    long long ans = sa[n - 1];
    for(int i = 0 ; i < n ; ++i){
        long long sum = sb[i];
        if(i + 1 < n){
            sum += sa[n - 1] - sa[i + 1];
            long long nbb = (ss[i] + s - 1) / s, nba = (ss[n - 1] - ss[i + 1] + s - 1) / s;
            if(nba + nbb > need) continue;
            long long lb = nbb * s - sb[i], la = 0;
            la = nba * s - (sa[n - 1] - sa[i + 1]);
            long long lft = need - nbb - nba;
            long long tmp = p[i + 1].s;
            if(p[i + 1].b >= p[i + 1].a){
                nbb += lft * s;
                sum += min(nbb, tmp) * p[i + 1].b;
                if(tmp > nbb) sum += (tmp - nbb) * p[i + 1].a;
            }else{
                nba += lft * s;
                sum += min(nba, tmp) * p[i + 1].a;
                if(tmp > nba) sum += (tmp - nba) * p[i + 1].b;
            }
        }
        ans = max(ans, sum);
    }
    cout << ans << '\n';
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
long long sa[maxn], sb[maxn], ss[maxn];
struct Person{
    long long a, b, s;
};

Person p[maxn];

int cmp (Person x, Person y){
    if(x.b - x.a == y.b - x.b) return x.b > y.b;
    return (x.b - x.a) > (y.b - y.a);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, s;
    cin >> n >> s;
    long long sm = 0;
    for(int i = 0 ; i < n ; ++i){
        cin >> p[i].s >> p[i].a >> p[i].b;
        sm += p[i].s;
    }
    sort(p, p + n, cmp);
    sa[0] = p[0].s * p[0].a;
    sb[0] = p[0].s * p[0].b;
    ss[0] = p[0].s;
    for(int i = 1 ; i < n ; ++i){
        sa[i] = p[i].s * p[i].a + sa[i - 1];
        sb[i] = p[i].s * p[i].b + sb[i - 1];
        ss[i] = p[i].s + ss[i - 1];
    }
    long long need = (sm + s - 1) / s;
    long long ans = sa[n - 1];
    for(int i = 0 ; i < n ; ++i){
        long long sum = sb[i];
        if(i + 1 < n){
            sum += sa[n - 1] - sa[i + 1];
            long long nbb = (ss[i] + s - 1) / s, nba = (ss[n - 1] - ss[i + 1] + s - 1) / s;
            if(nba + nbb > need) continue;
            long long lb = nbb * s - sb[i], la = 0;
            la = nba * s - (sa[n - 1] - sa[i + 1]);
            long long lft = need - nbb - nba;
            long long tmp = p[i + 1].s;
            if(p[i + 1].b >= p[i + 1].a){
                nbb += lft * s;
                sum += min(nbb, tmp) * p[i + 1].b;
                if(tmp > nbb) sum += (tmp - nbb) * p[i + 1].a;
            }else{
                nba += lft * s;
                sum += min(nba, tmp) * p[i + 1].a;
                if(tmp > nba) sum += (tmp - nba) * p[i + 1].b;
            }
        }
        ans = max(ans, sum);
    }
    cout << ans << '\n';
    return 0;

}


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
	long long int si,delta;
}a[100001],b[100001];
bool cmp(node a,node b)
{
	return a.delta>b.delta;
}
int main()
{
	long long int n,s,t,x,y,i;
	cin>>n>>s;
	long long int sum=0,sumas=0,sumbs=0,sums=0,sumss=0,sumds=0;
	long long int an=0,bn=0,minusa=0,minusb=0;
	for(i=0;i<n;i++)
	{
		cin>>t>>x>>y;
		if(x>=y)
		{
			a[an].si=t;
			a[an].delta=x-y;
			an++;
			sum+=t*x;
			sumas+=t;
		}
		if(y>x)
		{
			b[bn].si=t;
			b[bn].delta=y-x;
			bn++;
			sum+=t*y;
			sumbs+=t;
		}
	}
	sort(a,a+an,cmp);
	sort(b,b+bn,cmp);
	sumas%=s;
	sumbs%=s;
	if(sumas+sumbs>s)
	{
		cout<<sum<<endl;
		return 0;
	}
	for(i=an-1;i>=0;i--)
	{
		if(sumas==0) break;
		if(sumas>=a[i].si)
		{
			minusa+=a[i].si*a[i].delta;
			sumas-=a[i].si;
		}
		else
		{
			minusa+=sumas*a[i].delta;
			sumas=0;
		}
	}
	for(i=bn-1;i>=0;i--)
	{
		if(sumbs==0) break;
		if(sumbs>=b[i].si)
		{
			minusb+=b[i].si*b[i].delta;
			sumbs-=b[i].si;
		}
		else
		{
			minusb+=sumbs*b[i].delta;
			sumbs=0;
		}
	}
	sum-=min(minusa,minusb);
	cout<<sum<<endl;
	return 0;
}


/**
 *   author:  ks3rr
 *   created: shiva.shiva.shiva krishna:krishna:krishna
 *   Jai Shree Radha Krishna!
**/
#include "bits/stdc++.h"
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef long double ld;
ld pi = 3.141592653589793238;
#define nl cout << "\n";
#define pll pair<ll, ll>
#define eb emplace_back
#define pb push_back
#define xINF 2000000000
const ll INF = ll(2e18);
#define xmod 1000000007
#define xmod2 998244353
#ifndef ONLINE_JUDGE
  #define xd(x) cout << #x << " = " << x << "\n";
#else
  #define xd(x) 5;
#endif
ll mo(const ll input, const ll ceil)
{
  return input >= ceil ? input % ceil : input;
}

vector< pair<ll,ll> >a,b;
ll n,s,ans,na,nb,ta,tb;

void solve(int tc)
{
  ll ss,aa,bb;
  pair<ll,ll>k;
  cin>>n>>s;
  for(int i=1;i<=n;i++)
  {
    cin>>ss>>aa>>bb;
    if(aa>bb)
    {
      ans+=ss*aa;
      na+=ss;
      k.first=aa-bb;
      k.second=ss;
      a.push_back(k);
    }
    else
    {
      ans+=ss*bb;
      nb+=ss;
      k.first=bb-aa;
      k.second=ss;
      b.push_back(k);
    }
  }
  // cout<<na<<"     "<<nb<<endl;
  na%=s;nb%=s;
  // cout<<ans<<endl;
  if(na+nb>s)
  {
    cout<<ans<<"\n";
    return ;
  }
  sort(a.begin(),a.end());
  sort(b.begin(),b.end());
  for(int i=0;na;i++)
  {
    ta+=min(na,a[i].second)*a[i].first;
    na-=min(na,a[i].second);
    // cout<<"ta "<<ta<<"     "<<"na "<<na<<endl;
  }
  for(int i=0;nb;i++)
  {
    tb+=min(nb,b[i].second)*b[i].first;
    nb-=min(nb,b[i].second);
    // cout<<"tb "<<tb<<"     "<<"nb "<<nb<<endl;
  }
  ans-=min(ta,tb);
  cout<<ans<<"\n";
}

signed main()
{
  fio
  int tt = 1;
  //cin>>tt;
  for (int i = 0; i < tt; i++)
  {
    solve(i + 1);
  }
  return 0;
}

//1