#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100005;
long long N, S;
int main() {
  while (cin >> N >> S) {
    vector<pair<long long, long long>> one, two;
    long long ans = 0, sum1 = 0, sum2 = 0;
    for (int i = 0; i < N; ++i) {
      long long s, a, b;
      cin >> s >> a >> b;
      if (a > b) {
        sum1 += s;
        ans += a * s;
        one.emplace_back(make_pair(a - b, s));
      } else {
        sum2 += s;
        ans += b * s;
        two.emplace_back(make_pair(b - a, s));
      }
    }
    sum1 %= S;
    sum2 %= S;
    if (sum1 + sum2 > S)
      cout << ans << endl;
    else {
      sort(one.begin(), one.end());
      sort(two.begin(), two.end());
      long long diff1 = 0, diff2 = 0;
      for (auto& i : one) {
        diff1 += min(sum1, i.second) * i.first;
        sum1 -= min(sum1, i.second);
        if (sum1 == 0) break;
      }
      for (auto& i : two) {
        diff2 += min(sum2, i.second) * i.first;
        sum2 -= min(sum2, i.second);
        if (sum2 == 0) break;
      }
      cout << ans - min(diff1, diff2) << endl;
    }
  }
}
