#include <bits/stdc++.h>
template <class T>
inline void read(T &x) {
  int t;
  while (((t = getchar()) < '0' || t > '9') && t != '-')
    ;
  bool flag = false;
  if (t == '-') flag = true, t = getchar();
  x = t - '0';
  while ((t = getchar()) >= '0' && t <= '9') x = x * 10 + (t - '0');
  if (flag) x = -x;
}
using namespace std;
const int maxn = 100010;
struct Node {
  long long a, b;
  Node() {}
  Node(long long x, long long y) : a(x), b(y) {}
  bool operator<(Node B) const { return a < B.a; }
};
long long n, s, tot;
long long eat[maxn], a[maxn], b[maxn];
int main() {
  read(n), read(s);
  for (int i = 1; i <= n; ++i) {
    read(eat[i]), read(a[i]), read(b[i]);
    tot += eat[i];
  }
  eat[++n] = (tot + s - 1) / s * s - tot;
  vector<Node> a_to_b, b_to_a;
  long long ans = 0, cnta = 0, cntb = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] > b[i]) {
      a_to_b.push_back(Node(a[i] - b[i], eat[i]));
      ans += eat[i] * a[i];
      cnta += eat[i];
    } else {
      b_to_a.push_back(Node(b[i] - a[i], eat[i]));
      ans += eat[i] * b[i];
      cntb += eat[i];
    }
  }
  long long minu;
  long long extra, res;
  {
    extra = cnta % s;
    sort(a_to_b.begin(), a_to_b.end());
    res = 0;
    for (int i = 0; i < (int)a_to_b.size(); ++i) {
      Node &p = a_to_b[i];
      long long tmp = min(p.b, extra);
      res += tmp * p.a;
      extra -= tmp;
    }
    minu = res;
  }
  {
    extra = cntb % s;
    sort(b_to_a.begin(), b_to_a.end());
    res = 0;
    for (int i = 0; i < (int)b_to_a.size(); ++i) {
      Node &p = b_to_a[i];
      long long tmp = min(p.b, extra);
      res += tmp * p.a;
      extra -= tmp;
    }
    minu = min(minu, res);
  }
  cout << ans - minu << endl;
  return 0;
}
