#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
struct node {
  int num, val;
  bool operator<(const node& r) const { return val > r.val; }
} a[maxn];
int main() {
  int n, s;
  scanf("%d%d", &n, &s);
  long long ans = 0;
  long long total = 0;
  for (int i = 0; i < n; i++) {
    int sum, x, y;
    scanf("%d%d%d", &sum, &x, &y);
    a[i].num = sum;
    a[i].val = y - x;
    ans += 1LL * sum * x;
    total += a[i].num;
  }
  if (total % s) {
    a[n].num = s - total % s;
    a[n++].val = 0;
  }
  sort(a, a + n);
  long long temp = 0;
  long long temp_val = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].val > 0) {
      temp += a[i].num;
      temp_val += 1LL * a[i].num * a[i].val;
      if (temp >= s) {
        temp = temp % s;
        ans += temp_val;
        temp_val = 1LL * temp * a[i].val;
        ans -= temp_val;
      }
    } else {
      temp += a[i].num;
      temp_val += 1LL * a[i].num * a[i].val;
      if (temp >= s) {
        temp_val -= 1LL * a[i].num * a[i].val;
        temp_val += 1LL * (s + a[i].num - temp) * a[i].val;
        if (temp_val > 0) {
          ans += temp_val;
        }
        break;
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
