#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long a[maxn], b[maxn], s[maxn], S, ans, mx;
long long aS, bS, nS;
int eata[maxn], eatb[maxn];
int n, ncnt, acnt, bcnt;
bool cmpA(const int &A, const int &B) { return (a[A] - b[A]) < (a[B] - b[B]); }
bool cmpB(const int &A, const int &B) { return (b[A] - a[A]) < (b[B] - a[B]); }
long long upceil(long long x, long long a) { return (x - 1) / a + 1; }
int main() {
  scanf("%d%lld", &n, &S);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld%lld%lld", &s[i], &a[i], &b[i]);
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) ++ncnt, ans += s[i] * a[i], nS += s[i];
    if (a[i] > b[i]) aS += s[i], eata[++acnt] = i, ans += s[i] * a[i];
    if (a[i] < b[i]) bS += s[i], eatb[++bcnt] = i, ans += s[i] * b[i];
  }
  sort(eata + 1, eata + acnt + 1, cmpA);
  sort(eatb + 1, eatb + bcnt + 1, cmpB);
  if (upceil(aS, S) + upceil(bS, S) <= upceil(nS + aS + bS, S))
    printf("%lld", ans);
  else {
    long long nans = ans;
    if (aS % S + bS % S <= S) {
      nans = ans;
      long long left = aS % S;
      for (int i = 1; i <= acnt; ++i) {
        if (left > s[eata[i]])
          nans -= s[eata[i]] * (a[eata[i]] - b[eata[i]]), left -= s[eata[i]];
        else {
          nans -= (left) * (a[eata[i]] - b[eata[i]]);
          break;
        }
      }
      mx = max(mx, nans);
      nans = ans;
      left = bS % S;
      for (int i = 1; i <= bcnt; ++i) {
        if (left > s[eatb[i]])
          nans -= s[eatb[i]] * (b[eatb[i]] - a[eatb[i]]), left -= s[eatb[i]];
        else {
          nans -= (left) * (b[eatb[i]] - a[eatb[i]]);
          break;
        }
      }
      mx = max(mx, nans);
    } else {
      mx = max(mx, nans);
      nans = ans;
      long long left = S - aS;
      for (int i = 1; i <= acnt; ++i) {
        if (left > s[eata[i]])
          nans -= s[eata[i]] * (a[eata[i]] - b[eata[i]]), left -= s[eata[i]];
        else {
          nans -= (left) * (a[eata[i]] - b[eata[i]]);
          break;
        }
      }
      mx = max(mx, nans);
      nans = ans;
      left = S - bS;
      for (int i = 1; i <= bcnt; ++i) {
        if (left > s[eatb[i]])
          nans -= s[eatb[i]] * (b[eatb[i]] - a[eatb[i]]), left -= s[eatb[i]];
        else {
          nans -= (left) * (b[eatb[i]] - a[eatb[i]]);
          break;
        }
      }
      mx = max(mx, nans);
    }
    printf("%lld", mx);
  }
  return 0;
}
