#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7, maxn = 200005, maxm = 26, ln = 17;
inline void S(int& n) {
  n = 0;
  int ch = getchar();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
  }
  n = n * sign;
}
const int shift = 30, etf = mod - 1, LIM = (int)1e9;
const long long int inf = (long long int)1e18 + 1;
const double PI = (double)3.141592653589793238, EPSILON = 1e-10;
vector<pair<long long int, long long int> > va, vb;
int main() {
  int n;
  long long int s;
  cin >> n >> s;
  long long int init_ans = 0;
  long long int pa = 0, pb = 0;
  for (int i = 0; i < n; i++) {
    long long int r, a, b;
    scanf("%lld", &r);
    scanf("%lld", &a);
    scanf("%lld", &b);
    if (a >= b) {
      va.push_back(make_pair(a - b, r));
      init_ans += r * a;
      pa += r;
    } else {
      vb.push_back(make_pair(b - a, r));
      init_ans += r * b;
      pb += r;
    }
  }
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  long long int ansa = init_ans;
  long long int ansb = init_ans;
  long long int reqa =
      ((pa + s - 1) / s + (pb + s - 1) / s == (pa + pb + s - 1) / s) ? 0
                                                                     : pb % s;
  long long int reqb =
      ((pa + s - 1) / s + (pb + s - 1) / s == (pa + pb + s - 1) / s) ? 0
                                                                     : pa % s;
  int idx1 = 0;
  while (reqa) {
    long long int cnt = min(reqa, vb[idx1].second);
    ansa -= cnt * vb[idx1].first;
    reqa -= cnt;
    idx1++;
  }
  int idx2 = 0;
  while (reqb) {
    long long int cnt = min(reqb, va[idx2].second);
    ansb -= cnt * va[idx2].first;
    reqb -= cnt;
    idx2++;
  }
  cout << max(ansb, ansa) << endl;
  return 0;
}
