#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:9759095000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
using namespace std;
long long gcd(long long i, long long j) {
  if (j == 0)
    return i;
  else
    return gcd(j, i % j);
}
template <typename T>
inline T getint() {
  T val = 0;
  char c;
  bool neg = false;
  while ((c = getchar()) && !(c >= '0' && c <= '9')) {
    neg |= c == '-';
  }
  do {
    val = (val * 10) + c - '0';
  } while ((c = getchar()) && (c >= '0' && c <= '9'));
  return val * (neg ? -1 : 1);
}
const long long INF = -(1LL << 31);
const int mod = 1000000007;
const long double eps = 1e-12, pi = acos(-1);
const int maxN = 300100, maxT = 100010;
vector<long long> s, a, b, p;
bool cmp(const int& aa, const int& bb) { return b[bb] - a[bb] < b[aa] - a[aa]; }
long long n, ct, sum = 0, add;
long long get_chi(long long m) {
  long long res = 0, tmp = 0;
  bool f = true;
  for (int j = 0; j < p.size(); ++j) {
    int i = p[j];
    if (f) {
      tmp += s[i];
      if (m * ct <= tmp) {
        long long dd = tmp - m * ct;
        f = false;
        long long dd1 = s[i] - dd;
        res += dd1 * b[i] + dd * a[i];
        int jj = j - 1;
        long long smm = add;
        if (a[i] > b[i]) {
          res += min(add, dd1) * (a[i] - b[i]);
          smm -= min(add, dd1);
          while (jj >= 0 && smm > 0) {
            int ii = p[jj--];
            if (b[ii] >= a[ii]) break;
            long long tt = min(smm, s[ii]);
            res += tt * (a[ii] - b[ii]);
            smm -= tt;
          }
        }
      } else {
        res += s[i] * b[i];
      }
    } else {
      res += s[i] * a[i];
    }
  }
  return res;
}
int32_t main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cout.precision(11);
  cin >> n >> ct;
  s.assign(n, 0);
  b.assign(n, 0);
  a.assign(n, 0);
  p.assign(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> s[i] >> a[i] >> b[i];
    p[i] = i;
    sum += s[i];
  }
  sort(p.begin(), p.end(), cmp);
  long long d = 0, e = 1, count = (sum + ct - 1) / ct;
  add = count * ct - sum;
  long long tmp = 0;
  for (auto i : p) {
    if (b[i] >= a[i]) {
      tmp += s[i];
      d = tmp / ct;
      e = d + 1;
    } else {
      break;
    }
  }
  long long ans = 0;
  ans = max(ans, get_chi(d));
  if (e <= count) ans = max(ans, get_chi(e));
  cout << ans << endl;
  return 0;
}
