#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e5 + 10;
const int MOD = (int)1e9 + 7;
long long ab(long long x) { return x > 0 ? x : -x; }
struct node {
  long long s, a, b;
  void scan() { scanf("%lld%lld%lld", &s, &a, &b); }
  bool operator<(const node &x) const { return ab(a - b) > ab(x.a - x.b); }
} p[MAXN];
priority_queue<node> qa, qb;
int main() {
  int n, s;
  scanf("%d%d", &n, &s);
  long long sa, sb, sum;
  sa = sb = sum = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    p[i].scan();
    if (p[i].a >= p[i].b)
      sa += p[i].s, qa.push(p[i]);
    else
      sb += p[i].s, qb.push(p[i]);
    ans += 1ll * max(p[i].a, p[i].b) * p[i].s;
  }
  sum = sa + sb;
  if ((sa + s - 1) / s + (sb + s - 1) / s != (sum + s - 1) / s) {
    sa %= s;
    sb %= s;
    long long d1 = 0, d2 = 0;
    while (sa) {
      node now = qa.top();
      qa.pop();
      if (now.s >= sa) {
        d1 += sa * ab(now.a - now.b);
        sa = 0;
        break;
      } else {
        d1 += now.s * ab(now.a - now.b);
        sa -= now.s;
      }
    }
    while (sb) {
      node now = qb.top();
      qb.pop();
      if (now.s >= sb) {
        d2 += sb * ab(now.a - now.b);
        sb = 0;
        break;
      } else {
        d2 += now.s * ab(now.a - now.b);
        sb -= now.s;
      }
    }
    ans -= min(d1, d2);
  }
  printf("%lld\n", ans);
  return 0;
}
