#include <bits/stdc++.h>
using namespace std;
const int MM = 120000;
long long int m, n, k, p1, p2, p3, p, f;
struct guy {
  long long int s, a, b, diff;
};
guy guys[MM];
long long int ceel(long long int num, long long int den) {
  assert(den > 0);
  if (num % den) return (num / den) + 1;
  return (num / den);
}
int main() {
  long long int ans = 0;
  cin >> n >> m;
  long long int slix = 0;
  for (int i = 0; i < n; i++) {
    cin >> guys[i].s >> guys[i].a >> guys[i].b;
    guys[i].diff = guys[i].a - guys[i].b;
    slix += guys[i].s;
  }
  long long int pzz = ceel(slix, m);
  long long int lite = 0;
  for (int i = 0; i < n; i++)
    if (guys[i].diff == 0) lite += guys[i].s;
  long long int t1 = 0, t2 = 0;
  priority_queue<pair<long long int, long long int> > patb, pbta;
  for (int i = 0; i < n; i++) {
    if (guys[i].diff > 0) {
      t1 += guys[i].s;
      patb.push({guys[i].b - guys[i].a, guys[i].s});
    } else {
      t2 += guys[i].s;
      pbta.push({guys[i].a - guys[i].b, guys[i].s});
    }
    ans += max(guys[i].a, guys[i].b) * guys[i].s;
  }
  long long int curpzz = ceel(t1, m) + ceel(t2, m);
  long long int atb = (t1 % m), bta = (t2 % m);
  if (curpzz == pzz) {
    cout << ans << endl;
    return 0;
  } else {
    long long int u1 = 0;
    long long int ansatb = ans;
    while (u1 < atb) {
      pair<long long int, long long int> cur = patb.top();
      if (u1 + cur.second >= atb) {
        ansatb += cur.first * (atb - u1);
        u1 = atb;
      } else {
        u1 += cur.second;
        ansatb += cur.first * cur.second;
      }
      patb.pop();
    }
    long long int u2 = 0;
    long long int ansbta = ans;
    while (u2 < bta) {
      pair<long long int, long long int> cur = pbta.top();
      if (u2 + cur.second >= bta) {
        ansbta += cur.first * (bta - u2);
        u2 = bta;
      } else {
        u2 += cur.second;
        ansbta += cur.first * cur.second;
      }
      pbta.pop();
    }
    cout << max(ansbta, ansatb);
  }
  return 0;
}
