#include <bits/stdc++.h>
using namespace std;
struct node {
  long long s, x;
} t[200005];
int cmp(node a, node b) {
  if (a.x == b.x) return a.s > b.s;
  return a.x > b.x;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, s;
  cin >> n >> s;
  long long ans, sum, a, b;
  ans = sum = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> t[i].s >> a >> b;
    t[i].x = b - a;
    ans += t[i].s * a;
    sum += t[i].s;
  }
  if (sum % s) {
    t[++n].x = 0;
    t[n].s = s - sum % s;
  }
  sort(t + 1, t + 1 + n, cmp);
  long long ma = 0, ans1 = 0, num = 0;
  for (long long i = 1; i <= n; i++) {
    if (num + t[i].s >= s) {
      ma = max(ma, (s - num) * t[i].x + ans1);
      ma = max(ma, ((num + t[i].s) / s * s - num) * t[i].x + ans1);
    }
    ans1 += t[i].x * t[i].s;
    num = (num + t[i].s) % s;
  }
  cout << ans + ma << endl;
  return 0;
}
