#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > s;
vector<long long> s1;
vector<pair<long long, long long> > ss;
long long n, d, sum = 0, sum1 = 0, sum2 = 0, nmx, nmx1 = 0, nmx2 = 0, dd;
long long summ(long long nn) {
  if (nn < 0 || nn > nmx) {
    return 0;
  }
  long long cnt = nn * d;
  bool bb = 0;
  long long summ1 = sum1;
  for (long long i = n - 1; i >= 0; i--)
    if (cnt > 0) {
      if (cnt <= dd && bb == 0 && ss[i].first <= 0) {
        bb = 1;
        cnt = 0;
      }
      if (cnt > dd && bb == 0 && ss[i].first <= 0) {
        bb = 1;
        cnt -= dd;
      }
      if (ss[i].second <= cnt) {
        cnt -= ss[i].second;
        summ1 += ss[i].first * ss[i].second;
      } else {
        summ1 += ss[i].first * cnt;
        cnt = 0;
      }
    }
  return summ1;
}
int main() {
  cin >> n >> d;
  s.resize(n);
  s1.resize(n);
  ss.resize(n);
  for (long long i = 0; i < n; i++) {
    cin >> s1[i] >> s[i].first >> s[i].second;
    sum += s1[i];
    sum1 += s[i].first * s1[i];
    if (s[i].first > s[i].second)
      nmx1 += s1[i];
    else
      nmx2 += s1[i];
    ss[i].first = s[i].second - s[i].first;
    ss[i].second = s1[i];
  }
  nmx = (sum - 1) / d + 1;
  dd = nmx * d - sum;
  nmx1 = (nmx1 - 1) / d + 1;
  nmx2 = (nmx2 - 1) / d + 1;
  sort(ss.begin(), ss.end());
  long long zmx = 0;
  for (long long i = -2; i <= 2; i++) {
    if (summ(i + nmx2) > zmx) {
      zmx = summ(i + nmx2);
    }
  }
  cout << zmx;
  return 0;
}
