#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, inf = 1e8;
long long s[N], a[N], b[N];
long long sum, c1, c2, res1, res2, m1, m2;
vector<pair<long long, long long> > v1, v2;
int main() {
  long long n, ss;
  cin >> n >> ss;
  m1 = inf;
  m2 = inf;
  for (int i = 1; i <= n; i++) {
    cin >> s[i] >> a[i] >> b[i];
    sum += s[i];
    if (a[i] >= b[i]) {
      c1 += s[i];
      res1 += s[i] * a[i];
      v1.push_back({a[i] - b[i], s[i]});
    } else {
      c2 += s[i];
      res2 += s[i] * b[i];
      v2.push_back({b[i] - a[i], s[i]});
    }
  }
  long long res = (sum + ss - 1) / ss;
  if (res == (c1 + ss - 1) / ss + (c2 + ss - 1) / ss)
    cout << res1 + res2 << endl;
  else {
    res = res1 + res2;
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    long long rem1 = c1 - ((c1 + ss - 1) / ss - 1) * ss;
    long long rem2 = c2 - ((c2 + ss - 1) / ss - 1) * ss;
    long long tem = res, ans;
    while (rem1 > 0) {
      for (auto u : v1) {
        tem -= u.first * min(u.second, rem1);
        rem1 -= u.second;
        if (rem1 <= 0) break;
      }
      ans = tem;
    }
    tem = res;
    while (rem2 > 0) {
      for (auto u : v2) {
        tem -= u.first * min(u.second, rem2);
        rem2 -= u.second;
        if (rem2 <= 0) break;
      }
      ans = max(ans, tem);
    }
    cout << ans << endl;
  }
}
