#include <bits/stdc++.h>
using namespace std;
long long N, S;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<pair<long long, long long> > ordering;
  long long ans = 0, Smod = 0;
  cin >> N >> S;
  for (long long i = 0; i < N; i++) {
    long long requires, a, b;
    cin >>
    requires >> a >> b;
    Smod = (Smod + requires) % S;
    if (requires) {
      ordering.push_back({a - b, requires});
      ans += b * requires;
    }
  }
  if (Smod) ordering.push_back(make_pair(0, S - Smod));
  sort(ordering.begin(), ordering.end());
  long long curCount = 0, curAdd = 0;
  for (auto x : ordering) {
    long long diff = x.first, requires = x.second;
    if (curCount + requires >= S) {
      curAdd += diff * (S - curCount);
      requires -= (S - curCount);
      ans += max(curAdd, 0ll);
      ans += (requires / S) * max(S * diff, 0ll);
      curCount = requires % S;
      curAdd = curCount * diff;
    } else {
      curCount += requires;
      curAdd += requires * diff;
    }
  }
  cout << ans << "\n";
  return 0;
}
