#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > f, s;
int main() {
  long long int N, S, A, B, C, tot_A = 0, tot_B = 0, tot_s = 0, ans = 0;
  cin >> N >> S;
  for (int D = 1; D <= N; D++) {
    cin >> C >> A >> B;
    if (A > B)
      f.push_back(make_pair(A - B, C)), tot_A += C, ans += A * C;
    else
      s.push_back(make_pair(B - A, C)), tot_B += C, ans += B * C;
    tot_s += C;
  }
  if (ceil((double)tot_s / S) ==
      ceil((double)tot_A / S) + ceil((double)tot_B / S))
    return cout << ans, 0;
  sort(f.begin(), f.end());
  sort(s.begin(), s.end());
  int cur = 0;
  long long int ans1 = ans, ans2 = ans;
  long long int left = tot_A % S, tem;
  while (left) {
    tem = min(left, f[cur].second);
    left -= tem;
    ans1 -= f[cur].first * tem;
    cur++;
  }
  left = tot_B % S, cur = 0;
  while (left) {
    tem = min(left, s[cur].second);
    left -= tem;
    ans2 -= s[(int)cur].first * tem;
    cur++;
  }
  cout << max(ans1, ans2);
  return 0;
}
