#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 8;
long long n, S, s, a, b, tot, sum, cnt, T;
vector<pair<long long, long long> > v;
int main() {
  cin >> n >> S;
  for (int i = 0; i < n; i++) {
    cin >> s >> a >> b;
    if (s > 0) v.push_back({a - b, s});
    T += (b * s);
    sum += s;
    sum %= S;
  }
  if (sum > 0) v.push_back({0, S - sum});
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (S > v[i].second + cnt) {
      cnt += v[i].second;
      tot += (v[i].first * v[i].second);
    } else {
      tot += (v[i].first * (S - cnt));
      v[i].second -= (S - cnt);
      T += max(tot, 1LL * 0);
      T += (v[i].second / S) * max(S * v[i].first, 1LL * 0);
      cnt = v[i].second % S;
      tot = cnt * v[i].first;
    }
  }
  cout << T;
}
