#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, s;
  cin >> n >> s;
  int cnt[n + 1], a[n + 1], b[n + 1];
  long long total = 0;
  for (int i = 0; i < n; ++i) {
    cin >> cnt[i] >> a[i] >> b[i];
    total += cnt[i];
  }
  long long pizzas = (total + s - 1) / s;
  int rem = s * pizzas - total;
  cnt[n] = rem;
  a[n] = b[n] = 0;
  n++;
  long long cnt_a = 0, cnt_b = 0;
  long long res = 0;
  vector<pair<long long, long long>> a_to_b, b_to_a;
  for (int i = 0; i < n; ++i) {
    if (a[i] > b[i]) {
      cnt_a += cnt[i];
      res += cnt[i] * 1ll * a[i];
      a_to_b.push_back({a[i] - b[i], cnt[i]});
    } else {
      cnt_b += cnt[i];
      res += cnt[i] * 1ll * b[i];
      b_to_a.push_back({b[i] - a[i], cnt[i]});
    }
  }
  sort(a_to_b.begin(), a_to_b.end());
  sort(b_to_a.begin(), b_to_a.end());
  long long best = (long long)1e12;
  {
    long long sub = 0;
    long long extra = cnt_a % s;
    for (auto &p : a_to_b) {
      long long take = min(extra, p.second);
      extra -= take;
      sub += take * p.first;
    }
    best = min(best, sub);
  }
  {
    long long sub = 0;
    long long extra = cnt_b % s;
    for (auto &p : b_to_a) {
      long long take = min(extra, p.second);
      extra -= take;
      sub += take * p.first;
    }
    best = min(best, sub);
  }
  cout << res - best << endl;
  return 0;
}
