#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T poww(T b, T p) {
  long long a = 1;
  while (p) {
    if (p & 1) {
      a = (a * b);
    }
    p >>= 1;
    b = (b * b);
  }
  return a;
}
template <class T>
inline T gcd(T a, T b) {
  if (b > a) return gcd(b, a);
  return ((b == 0) ? a : gcd(b, a % b));
}
template <class T>
inline T modpoww(T b, T p, T modd) {
  long long a = 1;
  while (p) {
    if (p & 1) {
      a = (a * b) % modd;
    }
    p >>= 1;
    b = (b * b) % modd;
  }
  return a % modd;
}
std::vector<pair<long long, long long> > v;
int main() {
  int i, j;
  long long n, s, k;
  long long si, a, b;
  cin >> n >> s;
  long long ans = 0, tans = 0;
  k = 0;
  for ((i) = 0; (i) < (n); (i)++) {
    cin >> si >> a >> b;
    v.push_back(make_pair(b - a, si));
    tans += (si * a);
    k += si;
  }
  if (k % s) v.push_back(make_pair(0, s - (k % s)));
  sort(v.rbegin(), v.rend());
  long long diff = 0;
  long long ct = 0;
  ans = tans;
  for ((i) = 0; (i) < (v.size()); (i)++) {
    if (ct + v[i].second >= s) {
      ans = max(ans, tans + diff + (s - ct) * v[i].first);
      k = ct + v[i].second;
      ans = max(ans, tans + diff + (k - (k % s) - ct) * v[i].first);
    }
    diff = diff + v[i].first * 1LL * v[i].second;
    ct = (ct + v[i].second) % s;
  }
  cout << ans << "\n";
}
