#include <bits/stdc++.h>
using namespace std;
int64_t mod = 1e9 + 7;
int64_t mod1 = 1e9 + 6;
int64_t power(int64_t a, int64_t b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power((((a % mod) * (a % mod)) % mod), b / 2) % mod;
  else
    return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) %
           mod;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int64_t n, s;
  cin >> n >> s;
  int64_t a[n + 1], b[n + 1], c[n + 1];
  int64_t sum = 0;
  vector<pair<int64_t, int64_t>> v;
  int64_t x = 0, y = 0;
  for (int64_t i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i] >> c[i], sum += a[i];
    if (b[i] >= c[i])
      x += a[i];
    else
      y += a[i];
    v.push_back({b[i] - c[i], i});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  int64_t pi = (sum + s - 1) / s;
  int64_t mini = x / s;
  int64_t maxi = min(pi - y / s + 1, pi);
  int64_t ans = 0;
  for (int64_t i = mini; i <= maxi; ++i) {
    int64_t sl1 = i * s, te = 0;
    int64_t sl2 = pi * s - sl1;
    int64_t ini = n, in2 = -1;
    for (int64_t j = 0; j < n; ++j) {
      if (v[j].first < 0) {
        in2 = j;
        break;
      }
      int64_t in = v[j].second;
      if (a[in] <= sl1) {
        te += b[in] * (a[in]);
        sl1 -= a[in];
      } else {
        te += b[in] * sl1;
        te += c[in] * (a[in] - sl1);
        ini = j + 1;
        sl1 = 0;
        break;
      }
    }
    if (in2 != -1) {
      int64_t in3 = -1;
      for (int64_t j = n - 1; j >= in2; --j) {
        int64_t in = v[j].second;
        if (a[in] <= sl2) {
          te += c[in] * (a[in]);
          sl2 -= a[in];
        } else {
          te += c[in] * sl2;
          te += b[in] * (a[in] - sl2);
          if (j != in2) in3 = j - 1;
          sl2 = 0;
          break;
        }
      }
      if (in3 == -1) {
        ans = max(ans, te);
        continue;
      } else {
        for (int64_t j = in2; j <= in3; ++j)
          te += a[v[j].second] * b[v[j].second];
        ans = max(ans, te);
        continue;
      }
    }
    for (int64_t j = ini; j < n; ++j) {
      int64_t in = v[j].second;
      te += c[in] * a[in];
    }
    ans = max(ans, te);
  }
  cout << ans << '\n';
}
