#include <bits/stdc++.h>
using namespace std;
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  solve();
  return 0;
}
template <typename T>
void chmin(T& a, T b) {
  if (a > b) a = b;
}
template <typename T>
void chmax(T& a, T b) {
  if (a < b) a = b;
}
template <typename T>
void MACRO_rdv2_Init(long long n, T& t) {
  t.resize(n);
}
template <typename First, typename... Rest>
void MACRO_rdv2_Init(long long n, First& first, Rest&... rest) {
  first.resize(n);
  MACRO_rdv2_Init(n, rest...);
}
template <typename T>
void MACRO_rdv2_Scan(long long p, T& t) {
  std::cin >> t[p];
}
template <typename First, typename... Rest>
void MACRO_rdv2_Scan(long long p, First& first, Rest&... rest) {
  std::cin >> first[p];
  MACRO_rdv2_Scan(p, rest...);
}
template <typename T>
void wrv(const vector<T>& v) {
  for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) {
    if (__ii) cout << ' ';
    cout << v[__ii];
  }
  cout << '\n';
}
template <typename T>
void wrm(const vector<vector<T>>& v) {
  for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) {
    for (long long(__jj) = (0); (__jj) < (v[__ii].size()); ++(__jj)) {
      if (__jj) cout << ' ';
      cout << v[__ii][__jj];
    }
    cout << '\n';
  }
}
template <typename T>
void sc(T& x) {
  cin >> x;
}
template <typename Head, typename... Tail>
void sc(Head& head, Tail&... tail) {
  cin >> head;
  sc(tail...);
}
template <typename T>
void wr(const T& x) {
  cout << x << '\n';
}
template <typename Head, typename... Tail>
void wr(const Head& head, const Tail&... tail) {
  cout << head << ' ';
  wr(tail...);
}
template <typename T>
void wrf(const T& x) {
  cout << x << endl;
}
template <typename Head, typename... Tail>
void wrf(const Head& head, const Tail&... tail) {
  cout << head << ' ';
  wrf(tail...);
}
template <typename T>
void debug_out(const T& x) {
  cerr << x << '\n';
}
template <typename Head, typename... Tail>
void debug_out(const Head& head, const Tail&... tail) {
  cerr << head << ' ';
  debug_out(tail...);
}
template <typename... T>
void err(const T&... cod) {
  wr(cod...);
  exit(0);
}
void solve() {
  long long n, s;
  sc(n, s);
  vector<vector<long long>> a(n, vector<long long>(4));
  long long oth = 0, tot = 0;
  for (long long(i) = (0); (i) < (n); ++(i)) {
    for (long long(j) = (1); (j) < (4); ++(j)) cin >> a[i][j];
    a[i][0] = a[i][2] - a[i][3];
    oth += a[i][3] * a[i][1];
    tot += a[i][1];
  }
  if (tot % s != 0) {
    a.push_back({0, s - tot % s, 0, 0});
    ++n;
  }
  sort(a.rbegin(), a.rend());
  long long ans = oth;
  long long i = 0;
  while (i < n) {
    long long j = i;
    long long k = max(s, a[i][1] / s * s);
    while (j < n && k > 0) {
      long long pick = min(k, a[j][1]);
      oth -= a[j][3] * pick;
      oth += a[j][2] * pick;
      k -= pick;
      a[j][1] -= pick;
      if (a[j][1] == 0) ++j;
    }
    if (k == 0) chmax(ans, oth);
    i = j;
  }
  wr(ans);
}
