#include <bits/stdc++.h>
using namespace std;
long long n, s, first[100005], a[100005], b[100005], c[100005], ma, mb, e;
long long nra, nrb, sa, sb, nr, m, rez, rez1, rez2;
pair<long long, long long> ra[100005];
pair<long long, long long> rb[100005];
int cmp(pair<long long, long long> A, pair<long long, long long> B) {
  return A.first < B.first;
}
int main() {
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> first[i] >> a[i] >> b[i];
    if (a[i] > b[i]) {
      nra++;
      ra[nra].first = a[i] - b[i];
      sa += a[i] * first[i];
      ra[nra].second = first[i];
      ma += first[i];
    } else {
      nrb++;
      rb[nrb].first = b[i] - a[i];
      sb += b[i] * first[i];
      rb[nrb].second = first[i];
      mb += first[i];
    }
    nr += first[i];
  }
  sort(ra + 1, ra + nra + 1, cmp);
  sort(rb + 1, rb + nrb + 1, cmp);
  ma = ma % s;
  mb = mb % s;
  if (((!ma || !mb) || n == 1) || ma + mb > s) {
    cout << sa + sb;
    return 0;
  }
  long long rez1 = sa + sb, rez2 = sa + sb, aux;
  for (int i = 1; i <= nra && ma; i++) {
    rez1 -= ra[i].first * min(ra[i].second, ma);
    ma -= min(ra[i].second, ma);
  }
  for (int i = 1; i <= nrb && mb; i++) {
    rez2 -= rb[i].first * min(rb[i].second, mb);
    mb -= min(rb[i].second, mb);
  }
  cout << max(rez1, rez2);
}
