#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 100;
struct pizza {
  long long n;
  long long v1;
  long long v2;
};
bool comp(const struct pizza &p1, const struct pizza &p2) {
  return abs(p1.v1 - p1.v2) > abs(p2.v1 - p2.v2);
}
struct pizza p[MAX];
long long n, m;
long long cal(long long p1Sum, long long p2Sum);
int main(void) {
  while (cin >> n >> m) {
    long long p1Sum = 0;
    long long p2Sum = 0;
    for (int i = 0; i < n; i++) {
      scanf("%lld %lld %lld", &p[i].n, &p[i].v1, &p[i].v2);
      if (p[i].v1 > p[i].v2)
        p1Sum += p[i].n;
      else
        p2Sum += p[i].n;
    }
    sort(p, p + n, comp);
    long long totalPizza = (p1Sum + p2Sum + m - 1) / m;
    long long p1 = (p1Sum + m - 1) / m;
    long long p2 = (p2Sum + m - 1) / m;
    long long ans = 0;
    if (p1 + p2 == totalPizza)
      ans = cal(p1 * m, p2 * m);
    else
      ans = max(cal(p1 * m - m, p2 * m), cal(p1 * m, p2 * m - m));
    cout << ans << endl;
  }
  return 0;
}
long long cal(long long p1Sum, long long p2Sum) {
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (p[i].v1 > p[i].v2) {
      if (p[i].n <= p1Sum) {
        ans = ans + p[i].n * p[i].v1;
        p1Sum -= p[i].n;
      } else {
        ans = ans + (p[i].n - p1Sum) * p[i].v2 + p1Sum * p[i].v1;
        p2Sum = p2Sum - (p[i].n - p1Sum);
        p1Sum = 0;
      }
    } else {
      if (p[i].n <= p2Sum) {
        ans = ans + p[i].n * p[i].v2;
        p2Sum -= p[i].n;
      } else {
        ans = ans + (p[i].n - p2Sum) * p[i].v1 + p2Sum * p[i].v2;
        p1Sum = p1Sum - (p[i].n - p2Sum);
        p2Sum = 0;
      }
    }
  }
  return ans;
}
