#include <bits/stdc++.h>
using namespace std;
struct eater {
  int64_t s, a, b;
  bool operator<(const eater &other) const {
    return (a - b) < (other.a - other.b);
  }
};
int main() {
  uint64_t N, S;
  cin >> N >> S;
  eater *eaters = new eater[N];
  uint64_t totalSlices = 0, posSlices = 0, negSlices = 0;
  uint64_t totalHappiness = 0, happA = 0, happB = 0;
  for (int i = 0; i < N; i++) {
    eater &e = eaters[i];
    cin >> e.s >> e.a >> e.b;
    happA += e.s * e.a;
    happB += e.s * e.b;
    if (e.a > e.b)
      posSlices += e.s;
    else if (e.b > e.a)
      negSlices += e.s;
    totalSlices += e.s;
  }
  sort(eaters, eaters + N);
  int BIndex = 0;
  int AIndex = N - 1;
  auto consume = [&eaters, &happA, &happB, &posSlices, &negSlices, N](
                     int &iter, int step, int64_t useSlices,
                     bool useA) -> uint64_t {
    int64_t tempHappiness = 0;
    while (useSlices > 0) {
      assert(iter >= 0);
      assert(iter < N);
      eater &e = eaters[iter];
      int64_t used = min(e.s, useSlices);
      e.s -= used;
      useSlices -= used;
      int64_t happiness = (useA) ? e.a : e.b;
      happA -= used * e.a;
      happB -= used * e.b;
      if (e.a > e.b)
        posSlices -= used;
      else if (e.b > e.a)
        negSlices -= used;
      tempHappiness += used * happiness;
      if (e.s == 0) iter += step;
    }
    return tempHappiness;
  };
  uint64_t useSlices = (posSlices / S) * S;
  totalHappiness += consume(AIndex, -1, useSlices, true);
  totalSlices -= useSlices;
  useSlices = (negSlices / S) * S;
  totalHappiness += consume(BIndex, 1, useSlices, false);
  totalSlices -= useSlices;
  if (totalSlices > S) {
    useSlices = posSlices;
    totalHappiness += consume(AIndex, -1, useSlices, true);
    totalSlices -= useSlices;
    totalHappiness += consume(BIndex, 1, totalSlices, false);
    totalSlices -= useSlices;
  } else {
    if (happA > happB)
      totalHappiness += consume(AIndex, -1, totalSlices, true);
    else
      totalHappiness += consume(BIndex, 1, totalSlices, false);
  }
  cout << totalHappiness << endl;
}
