#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > atype, btype;
int main() {
  int n, s;
  cin >> n >> s;
  long long tot = 0, ans = 0, tota = 0, totb = 0;
  for (int i = 0; i < n; ++i) {
    int a, b, ss;
    scanf("%d%d%d", &ss, &a, &b);
    tot += ss;
    ans += 1ll * ss * max(a, b);
    if (a > b)
      atype.push_back({a - b, ss}), tota += ss;
    else
      btype.push_back({b - a, ss}), totb += ss;
  }
  sort(atype.begin(), atype.end());
  sort(btype.begin(), btype.end());
  long long num = (tot + s - 1) / s;
  if ((tota + s - 1) / s + (totb + s - 1) / s <= num) {
    cout << ans << endl;
    return 0;
  }
  tota %= s, totb %= s;
  long long sum1 = 0;
  for (int i = 0; i < atype.size(); ++i) {
    sum1 += min(1ll * atype[i].second, tota) * atype[i].first;
    tota -= atype[i].second;
    if (tota <= 0) break;
  }
  long long sum2 = 0;
  for (int i = 0; i < btype.size(); ++i) {
    sum2 += min(1ll * btype[i].second, totb) * btype[i].first;
    totb -= btype[i].second;
    if (totb <= 0) break;
  }
  cout << ans - min(sum1, sum2) << endl;
  return 0;
}
