#include <bits/stdc++.h>
using namespace std;
const long long inff = 0x3f3f3f3f3f3f3f3f;
long long n, s, zz, ss, qw, er;
struct as {
  long long x, y, z;
} a[100008];
bool cmp(as a, as b) { return (a.y - a.z) > (b.y - b.z); }
long long ass(long long x) {
  if (x < 0 || x > qw) return 0;
  priority_queue<pair<long long, long long> > q;
  long long ans = 0, df = er, y;
  x *= s;
  for (int i(1); i <= (n); ++i) {
    if (x) {
      if (x >= a[i].x) {
        x -= a[i].x, ans += a[i].y * a[i].x;
        if (a[i].z > a[i].y) q.push(pair<int, int>(a[i].z - a[i].y, a[i].x));
      } else {
        ans += x * a[i].y, ans += a[i].z * (a[i].x - x);
        if (a[i].z > a[i].y) q.push(pair<int, int>(a[i].z - a[i].y, x));
        x = 0;
      }
    } else
      ans += a[i].z * a[i].x;
  }
  while (!q.empty() && df) {
    pair<long long, long long> sd = q.top();
    q.pop();
    y = min(df, sd.second);
    ans += sd.first * y;
    df -= y;
  }
  return ans;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (int i(1); i <= (n); ++i) {
    scanf("%lld", &a[i].x), scanf("%lld", &a[i].y), scanf("%lld", &a[i].z),
        qw += a[i].x;
    if (a[i].y - a[i].z > 0) ss += a[i].x;
  }
  sort(a + 1, a + n + 1, cmp);
  er = qw;
  if (qw % s)
    qw = qw / s + 1;
  else
    qw /= s;
  er = qw * s - er;
  if (ss % s)
    ss = ss / s + 1;
  else
    ss /= s;
  cout << max(ass(ss), ass(ss - 1)) << endl;
  return 0;
}
