#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s;
  cin >> n >> s;
  vector<pair<long long, int> > sorted;
  vector<pair<long long, pair<long long, long long> > > vec;
  long long tot = 0;
  for (int i = 0; i < n; i++) {
    long long si, a, b;
    cin >> si >> a >> b;
    tot += si;
    vec.push_back({si, {a, b}});
    sorted.push_back({{abs(b - a)}, i});
  }
  sort(sorted.begin(), sorted.end(), greater<pair<long long, int> >());
  long long type1, type2, happiness;
  happiness = type1 = type2 = 0;
  vector<int> vec1, vec2;
  for (int i = 0; i < n; i++) {
    if (vec[sorted[i].second].second.first >
        vec[sorted[i].second].second.second) {
      type1 += vec[sorted[i].second].first;
      happiness +=
          vec[sorted[i].second].second.first * vec[sorted[i].second].first;
      vec1.push_back(sorted[i].second);
    } else {
      type2 += vec[sorted[i].second].first;
      happiness +=
          vec[sorted[i].second].second.second * vec[sorted[i].second].first;
      vec2.push_back(sorted[i].second);
    }
  }
  if (ceil(type1 * 1. / s) + ceil(type2 * 1. / s) > ceil(tot * 1. / s)) {
    long long ans1, ans2;
    ans1 = ans2 = happiness;
    long long rem = type1 % s;
    for (int i = vec1.size() - 1; i >= 0; i--) {
      if (rem <= 0) break;
      ans1 -= min(rem, vec[vec1[i]].first) *
              abs(vec[vec1[i]].second.first - vec[vec1[i]].second.second);
      rem -= min(rem, vec[vec1[i]].first);
    }
    rem = type2 % s;
    for (int i = vec2.size() - 1; i >= 0; i--) {
      if (rem <= 0) break;
      ans2 -= min(rem, vec[vec2[i]].first) *
              abs(vec[vec2[i]].second.first - vec[vec2[i]].second.second);
      rem -= min(rem, vec[vec2[i]].first);
    }
    cout << max(ans1, ans2);
  } else {
    cout << happiness;
  }
  return 0;
}
