#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, s, i;
  cin >> n >> s;
  long long int p, a, b, ha = 0, hb = 0, areq = 0, breq = 0, slices = 0;
  vector<pair<long long int, long long int> > al;
  vector<pair<long long int, long long int> > bl;
  for (i = 0; i < n; i++) {
    cin >> p >> a >> b;
    ha += p * a;
    hb += p * b;
    long long int diff = a - b;
    if (diff > 0) {
      al.push_back({diff, p});
      areq += p;
    } else if (diff < 0) {
      bl.push_back({-diff, p});
      breq += p;
    }
    slices += p;
  }
  long long int tp, asure, amay = 0, bsure, bmay = 0;
  tp = slices / s;
  if (slices % s) tp++;
  asure = areq / s;
  if (areq % s) amay++;
  bsure = breq / s;
  if (breq % s) bmay++;
  if (bmay == 0) {
    asure += amay;
    amay = 0;
  }
  if (amay == 0) {
    bsure += bmay;
    bmay = 0;
  }
  if ((amay == 0 && bmay == 0) || (asure + bsure + amay + bmay <= tp)) {
    long long int pro = 0, ans;
    for (i = 0; i < bl.size(); i++) pro += bl[i].first * bl[i].second;
    ans += ha + pro;
    return cout << ans, 0;
  }
  sort(al.begin(), al.end());
  sort(bl.begin(), bl.end());
  long long int nota = areq - asure * s;
  i = 0;
  long long int loss = 0, profit = 0, ans;
  while (1) {
    long long int x = al[i].second;
    if (nota > x) {
      loss += x * al[i].first;
      nota -= x;
    } else {
      loss += nota * al[i].first;
      nota = 0;
      break;
    }
    i++;
  }
  for (i = 0; i < bl.size(); i++) profit += bl[i].first * bl[i].second;
  ans = ha + profit - loss;
  long long int notb = breq - bsure * s, lossb = 0;
  i = 0;
  while (1) {
    long long int x = bl[i].second;
    if (notb > x) {
      lossb += x * bl[i].first;
      notb -= x;
    } else {
      lossb += notb * bl[i].first;
      notb = 0;
      break;
    }
    i++;
  }
  ans = max(ans, ha + profit - lossb);
  cout << ans;
  return 0;
}
