#include <bits/stdc++.h>
using namespace std;
const long long N = (long long)1e5;
long long n, sp, s[N + 1], a[N + 1], b[N + 1];
long long a_total = 0, b_total = 0, total = 0, ans = 0;
vector<pair<long long, long long> > h1, h2;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> sp;
  for (long long i = 0; i < n; i++) {
    cin >> s[i] >> a[i] >> b[i];
    total += s[i];
    ans += max(a[i], b[i]) * s[i];
  }
  long long np = (total + sp - 1) / sp;
  a[n] = b[n] = 0;
  s[n] = np * sp - total;
  n = n + 1;
  for (long long i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      a_total += s[i];
      h1.push_back(make_pair(a[i] - b[i], s[i]));
    } else {
      b_total += s[i];
      h2.push_back(make_pair(b[i] - a[i], s[i]));
    }
  }
  long long curr = 0;
  sort(h1.begin(), h1.end());
  long long extra = a_total % sp;
  for (auto x : h1) {
    long long exchange = min(extra, x.second);
    curr += x.first * exchange;
    extra -= exchange;
  }
  long long best = curr;
  curr = 0;
  sort(h2.begin(), h2.end());
  extra = b_total % sp;
  for (auto x : h2) {
    long long exchange = min(extra, x.second);
    curr += x.first * exchange;
    extra -= exchange;
  }
  best = min(best, curr);
  cout << (ans - best) << endl;
  return 0;
}
