#include <bits/stdc++.h>
using namespace std;
long long n, m;
struct zuo {
  long long val, l, r;
  bool operator<(const zuo &b) const { return (l - r) < (b.l - b.r); }
};
struct you {
  long long val, l, r;
  bool operator<(const you &b) const { return r - l < (b.r - b.l); }
};
struct fc {
  long long num, l, r;
};
vector<zuo> a;
vector<you> b;
vector<fc> store;
int main() {
  scanf("%lld%lld", &n, &m);
  long long ans = 0;
  long long cntzuo = 0, cntyou = 0;
  for (int i = 0; i < n; i++) {
    long long val, l, r;
    scanf("%lld%lld%lld", &val, &l, &r);
    if (l < r) {
      b.push_back({val, l, r});
      cntyou += val;
      ans += val * r;
    } else {
      a.push_back({val, l, r});
      ans += val * l;
      cntzuo += val;
    }
  }
  cntzuo %= m;
  cntyou %= m;
  if (cntzuo + cntyou > m) {
    cout << ans << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  int l = 0;
  while (cntzuo && l < a.size()) {
    if (a[l].val >= cntzuo) {
      ans -= cntzuo * a[l].l;
      store.push_back({cntzuo, a[l].l, a[l].r});
      cntzuo = 0;
    } else {
      ans -= a[l].val * a[l].l;
      cntzuo -= a[l].val;
      store.push_back({a[l].val, a[l].l, a[l].r});
      l++;
    }
  }
  l = 0;
  while (cntyou && l < b.size()) {
    if (b[l].val >= cntyou) {
      ans -= cntyou * b[l].r;
      store.push_back({cntyou, b[l].l, b[l].r});
      cntyou = 0;
    } else {
      ans -= b[l].val * b[l].r;
      cntyou -= b[l].val;
      store.push_back({b[l].val, b[l].l, b[l].r});
      l++;
    }
  }
  long long tmpl = 0, tmpr = 0;
  for (auto elem : store) {
    tmpl += elem.l * elem.num;
    tmpr += elem.r * elem.num;
  }
  ans += max(tmpl, tmpr);
  cout << ans << endl;
  return 0;
}
