#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, int> &p1, const pair<int, int> &p2) {
  if (p1.first == p2.first)
    return p1.second < p2.second;
  else
    return p1.first < p2.first;
}
int main() {
  long long n, S;
  cin >> n >> S;
  vector<long long> s(n), a(n), b(n);
  long long sum = 0, suma = 0, sumb = 0;
  for (long long i = 0; i < n; i++) {
    cin >> s[i] >> a[i] >> b[i];
    sum += s[i];
  }
  vector<pair<long long, long long> > sa, sb;
  long long happy = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] >= b[i]) {
      sa.push_back(make_pair((a[i] - b[i]), i));
      suma += s[i];
      happy += s[i] * a[i];
    } else {
      sb.push_back(make_pair((b[i] - a[i]), i));
      sumb += s[i];
      happy += s[i] * b[i];
    }
  }
  sort(sa.begin(), sa.end());
  sort(sb.begin(), sb.end());
  long long actual = (long long)(sum + S - 1) / S;
  long long req = (long long)(suma + S - 1) / S + (long long)(sumb + S - 1) / S;
  if (req <= actual)
    cout << happy << endl;
  else {
    long long difa = suma - (suma / S) * S;
    long long difb = sumb - (sumb / S) * S;
    long long ans = happy, ans1 = happy;
    int i = 0;
    while (difa > 0 and i < sa.size()) {
      if (s[sa[i].second] >= difa) {
        ans -= sa[i].first * difa;
        difa = 0;
      } else {
        ans -= sa[i].first * s[sa[i].second];
        difa -= s[sa[i].second];
      }
      i++;
    }
    i = 0;
    while (difb > 0 and i < sb.size()) {
      if (s[sb[i].second] >= difb) {
        ans1 -= sb[i].first * difb;
        difb = 0;
      } else {
        ans1 -= sb[i].first * s[sb[i].second];
        difb -= s[sb[i].second];
      }
      i++;
    }
    cout << max(ans, ans1);
  }
  return 0;
}
