#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
long long n, a[N], b[N], S, s[N], f[N], p[N], smod, opt, ccnt, csum, i;
bool cmp(int a, int b) { return f[a] < f[b]; }
int main(int argc, char *argv[]) {
  cin >> n >> S;
  for (i = 1; i <= n; ++i) {
    cin >> s[i] >> a[i] >> b[i];
    f[i] = a[i] - b[i];
    p[i] = i;
    opt += b[i] * s[i];
    smod += s[i];
  }
  if (smod %= S) {
    ++n;
    p[n] = n;
    f[n] = 0;
    s[n] = S - smod;
  }
  sort(p + 1, p + n + 1, cmp);
  for (i = 1; i <= n; ++i) {
    long long va = f[p[i]], vs = s[p[i]];
    if (ccnt + vs >= S) {
      csum += va * (S - ccnt);
      vs -= S - ccnt;
      opt += max(csum, 0ll);
      opt += (vs / S) * max(S * va, 0ll);
      ccnt = vs % S;
      csum = ccnt * va;
    } else
      ccnt += vs, csum += vs * va;
  }
  cout << opt << endl;
  return 0;
}
