#include <bits/stdc++.h>
using namespace std;
const int maxn = 100003;
struct node {
  long long num, p;
} c[maxn], d[maxn];
long long n, S, num1 = 0ll, num2 = 0ll;
long long s[maxn] = {0}, a[maxn] = {0}, b[maxn] = {0};
bool cmp(node a, node b) { return a.p > b.p; }
int main() {
  long long sum = 0ll, ans = 0ll;
  scanf("%lld%lld", &n, &S);
  for (long long i = 1; i <= n; ++i) {
    scanf("%lld%lld%lld", &s[i], &a[i], &b[i]);
    sum += s[i];
    ans += min(a[i], b[i]) * s[i];
    long long t = min(a[i], b[i]);
    a[i] -= t;
    b[i] -= t;
    if (a[i] > 0ll) {
      c[++num1].num = s[i];
      c[num1].p = a[i];
    } else {
      d[++num2].num = s[i];
      d[num2].p = b[i];
    }
  }
  if (num1) sort(c + 1, c + num1 + 1, cmp);
  if (num2) sort(d + 1, d + num2 + 1, cmp);
  long long temp = 0, tem = 0;
  long long num = (sum + S - 1) / S;
  for (long long j = 1ll; j <= num1; ++j) tem += c[j].num;
  long long l = tem / S;
  for (long long i = l - 1; i <= l + 1; ++i) {
    long long t1 = i * S, t2 = S * num - t1;
    long long tem = 0;
    for (long long j = 1ll; j <= num1; ++j)
      if (t1) {
        tem += min(c[j].num, t1) * c[j].p;
        t1 -= min(c[j].num, t1);
      }
    for (long long j = 1ll; j <= num2; ++j)
      if (t2) {
        tem += min(d[j].num, t2) * d[j].p;
        t2 -= min(d[j].num, t2);
      }
    temp = max(temp, tem);
  }
  printf("%lld\n", ans + temp);
  return 0;
}
