#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long num = 0, num1 = 0, num2 = 0;
long long ans = 0;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    p1, p2;
void init() {
  for (int i = 1; i <= n; ++i) {
    long long s, a, b;
    scanf("%lld%lld%lld", &s, &a, &b);
    num += s;
    if (a >= b) {
      num1 += s;
      ans += s * a;
      p1.push(pair<long long, long long>(a - b, s));
    } else {
      num2 += s;
      ans += s * b;
      p2.push(pair<long long, long long>(b - a, s));
    }
  }
}
void solve() {
  long long tmp1 = 0, tmp2 = 0;
  long long t = num1 % m;
  while (!p1.empty()) {
    pair<long long, long long> x = p1.top();
    p1.pop();
    if (t >= x.second) {
      tmp1 += x.first * x.second;
      t -= x.second;
    } else {
      tmp1 += t * x.first;
      break;
    }
  }
  t = num2 % m;
  while (!p2.empty()) {
    pair<long long, long long> x = p2.top();
    p2.pop();
    if (t >= x.second) {
      tmp2 += x.first * x.second;
      t -= x.second;
    } else {
      tmp2 += t * x.first;
      break;
    }
  }
  printf("%lld", ans - min(tmp1, tmp2));
}
int main() {
  scanf("%lld%lld", &n, &m);
  init();
  long long t = (num / m + (num % m == 0 ? 0 : 1));
  long long t1 = (num1 / m + (num1 % m == 0 ? 0 : 1));
  long long t2 = (num2 / m + (num2 % m == 0 ? 0 : 1));
  if (t1 + t2 > t) {
    solve();
  } else {
    printf("%lld", ans);
  }
  return 0;
}
