#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
pair<pair<int, int>, pair<int, int> > ar[N];
long long n, s, all, ost, sum;
long long check(long long h) {
  long long a = h * s, rez = 0, tmp = ost;
  for (int i = 0; i < n; ++i) {
    long long need = ar[i].first.second;
    if (a) {
      if (ar[i].first.first > 0 && tmp) {
        a -= min(a, tmp);
        tmp = 0;
      }
      long long cur = min(a, need);
      a -= cur;
      rez += (long long)ar[i].second.first * cur;
      need -= cur;
    }
    if (need) rez += ar[i].second.second * need;
  }
  return rez;
}
int main() {
  cin >> n >> s;
  for (int i = 0, si, ai, bi; i < n; ++i) {
    scanf("%d%d%d", &si, &ai, &bi);
    ar[i] = {{bi - ai, si}, {ai, bi}};
    sum += si;
  }
  all = (sum + s - 1) / s;
  ost = all * s - sum;
  sort(ar, ar + n);
  long long l = 0, r = all, h1, h2;
  sum = 0;
  for (int i = 0; i < n; ++i) {
    if (ar[i].first.first < 0) sum += ar[i].first.second;
  }
  r = (sum + s - 1) / s;
  l = max(0LL, r - 1);
  cout << max(check(l), check(r));
  return 0;
}
