#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& n) {
  char ch;
  int sign = 1;
  while (!isdigit(ch = getchar()))
    if (ch == '-') sign = -1;
  n = ch - '0';
  while (isdigit(ch = getchar())) n = n * 10 + ch - '0';
  n *= sign;
}
const int INF = 1e9 + 7;
const int N = 122222;
long long n, m, sum, tt, cnt, pie;
long long s[N], a[N], b[N], pre[N];
pair<long long, long long> c[N], tc[N];
long long run(long long first) {
  first -= (pie * m - sum);
  for (int i = (1); i <= (tt); ++i) tc[i] = c[i];
  long long tcnt = cnt, ans = 0;
  int pos = 1;
  for (; pos <= tt && first >= tc[pos].second; ++pos)
    tcnt += tc[pos].first * tc[pos].second, first -= tc[pos].second;
  if (first > 0 && pos <= tt)
    tcnt += first * tc[pos].first, tc[pos].second -= first, first = 0;
  ans = max(ans, tcnt);
  for (int i = 1; i <= pie * m - sum; i++) {
    for (first++; pos <= tt && first >= tc[pos].second; ++pos)
      tcnt += tc[pos].first * tc[pos].second, first -= tc[pos].second;
    if (first > 0 && pos <= tt)
      tcnt += first * tc[pos].first, tc[pos].second -= first, first = 0;
    ans = max(ans, tcnt);
  }
  return ans;
}
int main() {
  read(n), read(m);
  for (int i = (1); i <= (n); ++i) {
    read(s[i]), read(a[i]), read(b[i]), sum += s[i];
    cnt += s[i] * a[i];
    c[++tt] = make_pair(b[i] - a[i], s[i]);
  }
  sort(c + 1, c + tt + 1), reverse(c + 1, c + tt + 1);
  for (int i = (1); i <= (tt); ++i) pre[i] = pre[i - 1] + c[i].second;
  pie = (long long)ceil(1. * sum / m);
  long long ans = cnt, first;
  int pos = 1;
  for (; pos <= tt && c[pos].first > 0; ++pos)
    ;
  pos--;
  first = (long long)ceil(1. * pre[pos] / m) * m;
  ans = max(ans, max(run(first), run(first - m)));
  cout << ans << endl;
  return 0;
}
