#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, pair<long long int, long long int> > > type1, type2;
bool cmp(pair<long long int, pair<long long int, long long int> > x,
         pair<long long int, pair<long long int, long long int> > y) {
  if (abs(x.second.first - x.second.second) >=
      abs(y.second.first - y.second.second))
    return 0;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long int n, s;
  cin >> n >> s;
  long long int t1, t2, sum1, sum2;
  t1 = t2 = sum1 = sum2 = 0;
  for (int i = 0; i < n; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    if (b >= c) {
      type1.push_back(make_pair(a, make_pair(b, c)));
      t1 += a;
      sum1 += a * b;
    } else {
      type2.push_back(make_pair(a, make_pair(b, c)));
      t2 += a;
      sum2 += a * c;
    }
  }
  long long int pizza1, pizza2, pizza;
  pizza1 = (t1 / s) + ((t1 % s) != 0);
  pizza2 = (t2 / s) + ((t2 % s) != 0);
  pizza = (t1 + t2) / s + (((t1 + t2) % s) != 0);
  if (pizza1 + pizza2 == pizza) {
    cout << sum1 + sum2 << '\n';
    return 0;
  }
  sort(type1.begin(), type1.end(), cmp);
  sort(type2.begin(), type2.end(), cmp);
  long long int ans1, ans2;
  long long int rem1 = t1 % s;
  long long int rem2 = t2 % s;
  long long int extra = s * pizza - (t1 + t2);
  long long int tmp1 = sum1, tmp2 = sum2;
  for (int i = 0; i < type1.size(); i++) {
    if (rem1 <= 0) break;
    if (type1[i].first <= rem1) {
      rem1 -= type1[i].first;
      tmp1 -= type1[i].first * (type1[i].second.first - type1[i].second.second);
    } else {
      tmp1 -= rem1 * (type1[i].second.first - type1[i].second.second);
      rem1 = 0;
    }
  }
  ans1 = sum2 + tmp1;
  for (int i = 0; i < type2.size(); i++) {
    if (rem2 <= 0) break;
    if (type2[i].first <= rem2) {
      rem2 -= type2[i].first;
      tmp2 -= type2[i].first * (type2[i].second.second - type2[i].second.first);
    } else {
      tmp2 -= rem2 * (type2[i].second.second - type2[i].second.first);
      rem2 = 0;
    }
  }
  ans2 = sum1 + tmp2;
  cout << max(ans1, ans2) << '\n';
  return 0;
}
