#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> V1, V2;
int main() {
  long long N, S;
  cin >> N >> S;
  long long s, a, b;
  long long ans = 0;
  long long p = 0, q = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%lld%lld%lld", &s, &a, &b);
    if (a > b) {
      ans += s * a;
      p += s;
      p %= S;
      V1.push_back({a - b, s});
    } else {
      ans += s * b;
      q += s;
      q %= S;
      V2.push_back({b - a, s});
    }
  }
  if (p + q > S) {
    cout << ans << endl;
    return 0;
  }
  sort(V1.begin(), V1.end());
  sort(V2.begin(), V2.end());
  long long x = 0, y = 0;
  for (auto it : V1) {
    x += min(it.second, p) * it.first;
    p -= min(it.second, p);
  }
  for (auto it : V2) {
    y += min(it.second, q) * it.first;
    q -= min(it.second, q);
  }
  cout << ans - min(x, y) << endl;
  return 0;
}
