#include <bits/stdc++.h>
using namespace std;
struct g {
  long long s, a, b;
} a[100050];
bool cmp(g a, g b) { return a.b > b.b; }
long long n, m;
int main() {
  scanf("%lld%lld", &n, &m);
  long long sum = 0;
  long long res = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &a[i].s, &a[i].a, &a[i].b);
    res += a[i].s * a[i].a;
    a[i].b -= a[i].a;
    sum += a[i].s;
  }
  sort(a, a + n, cmp);
  int i = 0;
  long long tmp = 0, tmp1 = 0;
  long long res1 = res;
  while (i < n && a[i].b > 0) {
    if (tmp1 + a[i].s >= m) {
      tmp1 = (tmp1 + a[i].s) % m;
      tmp += (a[i].s - tmp1) * a[i].b;
      res1 += tmp;
      tmp = tmp1 * a[i].b;
    } else {
      tmp += a[i].s * a[i].b;
      tmp1 += a[i].s;
    }
    i++;
  }
  tmp1 += (m - sum % m) % m;
  while (i < n && tmp1 < m) {
    if (tmp1 + a[i].s >= m) {
      tmp += (m - tmp1) * a[i].b;
      tmp1 = m;
    } else {
      tmp += a[i].s * a[i].b;
      tmp1 += a[i].s;
    }
    i++;
  }
  if (tmp > 0) res1 += tmp;
  res = max(res, res1);
  printf("%lld\n", res);
}
