#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
long long n, S;
long long s[N], a[N], b[N];
struct node {
  long long x;
  int id;
  bool operator<(const node& r) const { return x < r.x; }
} c[N];
int main() {
  int i, f = 0;
  long long ans = 0, sum = 0, a1 = 0, a2 = 0;
  long long s1 = 0, s2 = 0;
  scanf("%lld %lld", &n, &S);
  for (i = 1; i <= n; ++i) {
    scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
    if (a[i] > b[i])
      s1 += s[i];
    else if (a[i] < b[i])
      s2 += s[i];
    sum += s[i];
    ans += max(a[i], b[i]) * s[i];
    c[i].x = a[i] - b[i];
    c[i].id = i;
  }
  long long cnt = (sum + S - 1) / S;
  if ((s1 + S - 1) / S + (s2 + S - 1) / S <= cnt) {
    printf("%lld", ans);
    return 0;
  }
  sort(c + 1, c + 1 + n);
  s1 %= S;
  s2 %= S;
  for (i = 1; i <= n; ++i) {
    if (c[i].x <= 0) {
      f = i;
      continue;
    }
    if (!s1) break;
    long long t = min(s[c[i].id], s1);
    a1 += t * c[i].x;
    s1 -= t;
  }
  for (i = f; i >= 1; --i) {
    if (!c[i].x) continue;
    if (!s2) break;
    long long t = min(s[c[i].id], s2);
    a2 -= t * c[i].x;
    s2 -= t;
  }
  printf("%lld\n", ans - min(a1, a2));
  return 0;
}
