#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > p;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 0, q1 = 0, q2 = 0, t1 = 0, t2 = 0, n, s, i, j, y, z, r, ans;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    cin >> j >> y >> z;
    t += j;
    p.push_back(make_pair(y - z, j));
    if (y >= z) {
      q1 += j * y;
      t1 += j;
    } else {
      q2 += j * z;
      t2 += j;
    }
  }
  if (t / s + (t % s != 0) == t1 / s + (t1 % s != 0) + t2 / s + (t2 % s != 0))
    cout << q1 + q2;
  else {
    ans = q1 + q2;
    sort(p.begin(), p.end());
    j = t2 % s;
    r = 0;
    for (i = 0; i < n; i++)
      if (p[i].first >= 0) break;
    t = i;
    i--;
    while (j > 0 && i >= 0) {
      r += (p[i].first * min(j, p[i].second));
      j = j - (p[i].second);
      i--;
    }
    ans = q1 + q2 + r;
    j = t1 % s;
    i = t;
    r = 0;
    while (j > 0 && i < n) {
      r += (p[i].first * min(j, p[i].second));
      j = j - (p[i].second);
      i++;
    }
    ans = max(ans, q1 + q2 - r);
    cout << ans;
  }
  return 0;
}
