#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, s;
  long long v[100005], a[100005], b[100005];
  long long sum = 0;
  vector<pair<pair<long long, long long>, pair<long long, long long> > > vvv;
  scanf("%lld %lld", &n, &s);
  for (int i = 0; i < n; i++) {
    scanf("%lld %lld %lld", v + i, a + i, b + i);
    vvv.push_back(
        make_pair(make_pair(abs(a[i] - b[i]), v[i]), make_pair(a[i], b[i])));
    sum += v[i];
  }
  sort(vvv.begin(), vvv.end());
  long long qtd = ceil((sum) / (double)s);
  long long ans = 0;
  long long qt1 = 0, qt2 = 0, tt = 0;
  for (int i = 0; i < n; i++) {
    if (vvv[i].second.first > vvv[i].second.second) {
      qt1 += vvv[i].first.second;
      ans += vvv[i].second.first * vvv[i].first.second;
    } else if (vvv[i].second.first < vvv[i].second.second) {
      qt2 += vvv[i].first.second;
      ans += vvv[i].second.second * vvv[i].first.second;
    } else {
      tt += vvv[i].first.second;
      ans += vvv[i].second.second * vvv[i].first.second;
    }
  }
  long long ans1 = ans, ans2 = ans;
  long long qtt1 = qt1 + tt, qtt2 = qt2;
  int id = 0;
  while (ceil((qtt1) / (double)s) + ceil((qtt2) / (double)s) > qtd) {
    if (vvv[id].second.first >= vvv[id].second.second) {
      if (vvv[id].first.second >= (qtt1) % s) {
        long long aux = (qtt1) % s;
        qtt1 -= aux;
        ans1 -= vvv[id].second.first * aux;
        qtt2 += aux;
        ans1 += vvv[id].second.second * aux;
        continue;
      }
      qtt1 -= vvv[id].first.second;
      ans1 -= vvv[id].second.first * vvv[id].first.second;
      qtt2 += vvv[id].first.second;
      ans1 += vvv[id].second.second * vvv[id].first.second;
    }
    id++;
  }
  id--;
  qtt1 = qt1;
  qtt2 = qt2 + tt;
  id = 0;
  while (ceil((qtt1) / (double)s) + ceil((qtt2) / (double)s) > qtd) {
    if (vvv[id].second.first <= vvv[id].second.second) {
      if (vvv[id].first.second >= (qtt2) % s) {
        long long aux = (qtt2) % s;
        qtt2 -= aux;
        ans2 -= vvv[id].second.second * aux;
        qtt1 += aux;
        ans2 += vvv[id].second.first * aux;
        continue;
      }
      qtt2 -= vvv[id].first.second;
      ans2 -= vvv[id].second.second * vvv[id].first.second;
      qtt1 += vvv[id].first.second;
      ans2 += vvv[id].second.first * vvv[id].first.second;
    }
    id++;
  }
  id--;
  printf("%lld\n", max(ans1, ans2));
  return 0;
}
