#include <bits/stdc++.h>
using namespace std;
const long long int N = 250005, mod = 31, bit = 60;
struct cont {
  long long int fa, fb, need, change;
};
long long int solve(vector<cont> a, long long int s, long long int ofa,
                    long long int sum) {
  long long int n = a.size(), i, ans = 0;
  long long int ofb = sum - ofa;
  ofa *= s;
  ofb *= s;
  bool isa = 1;
  for (i = n - 1; i >= 0; i--) {
    if (isa) {
      if (ofa >= a[i].need) {
        ofa -= a[i].need;
        ans += (a[i].need * a[i].fa);
        a[i].need = 0;
      } else {
        a[i].need -= ofa;
        ans += (ofa * a[i].fa);
        isa = 0;
        i++;
      }
    } else {
      assert(ofb >= a[i].need);
      ofb -= a[i].need;
      ans += (a[i].need * a[i].fb);
      a[i].need = 0;
    }
  }
  return ans;
}
bool comp(cont &a, cont &b) {
  if (a.change != b.change) {
    return a.change < b.change;
  }
  if (a.fa != b.fa) {
    return a.fa < b.fa;
  }
  if (a.fb != b.fb) {
    return a.fb > b.fb;
  }
  return a.need > b.need;
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int pro = 1, temp, t, i, j, l, r, n, m, x, y, z, mid, k, rem,
                carry = 0, ind, ans = 0, cnt = 0, curr = 0, prev, next, sum = 0,
                i1 = -1, i2 = -1;
  long long int s;
  cin >> n >> s;
  vector<cont> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i].need >> a[i].fa >> a[i].fb;
    a[i].change = a[i].fa - a[i].fb;
    sum += a[i].need;
  }
  temp = sum;
  sum = (sum + s - 1) / s;
  l = 0, r = sum;
  cont tm;
  tm.fa = tm.fb = tm.change = 0;
  tm.need = (sum * s - temp);
  a.push_back(tm);
  sort(a.begin(), a.end(), comp);
  long long int mid1, mid2;
  while (l <= r) {
    x = (r - l) / 3;
    mid1 = l + x;
    mid2 = r - x;
    i1 = solve(a, s, mid1, sum);
    i2 = solve(a, s, mid2, sum);
    if (i2 >= i1) {
      ans = max(ans, i2);
      l = mid1 + 1;
    } else {
      ans = max(ans, i1);
      r = mid2 - 1;
    }
  }
  cout << ans << '\n';
  return 0;
}
