#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long N, S;
  cin >> N >> S;
  long long s[N + 1], a[N + 1], b[N + 1];
  long long type1 = 0, type2 = 0, happiness = 0;
  vector<pair<long long, long long> > AtoB, BtoA;
  for (int i = 1; i <= N; i++) {
    cin >> s[i] >> a[i] >> b[i];
    if (a[i] >= b[i]) {
      type1 += s[i];
      happiness += s[i] * a[i];
      AtoB.push_back({a[i] - b[i], s[i]});
    } else {
      type2 += s[i];
      happiness += s[i] * b[i];
      BtoA.push_back({b[i] - a[i], s[i]});
    }
  }
  sort(AtoB.begin(), AtoB.end());
  sort(BtoA.begin(), BtoA.end());
  long long x = type1 % S, y = type2 % S, z = happiness;
  if ((S - x) % S + (S - y) % S < S) {
    cout << happiness;
    return 0;
  }
  long long AB = min(x, (S - y) % S), BA = min(y, (S - x) % S);
  long long u = 0, v = 0;
  for (auto w : AtoB) {
    if (w.second < AB) {
      AB -= w.second;
      u += w.first * w.second;
    } else {
      u += AB * w.first;
      AB = 0;
      break;
    }
  }
  for (auto w : BtoA) {
    if (w.second < BA) {
      BA -= w.second;
      v += w.first * w.second;
    } else {
      v += w.first * BA;
      BA = 0;
      break;
    }
  }
  cout << happiness - min(u, v);
}
