#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int d = 1e5;
vector<pair<int, int> > p;
int main() {
  int N, S;
  scanf("%d%d", &N, &S);
  long long sum = 0, ans = 0, sums = 0, d = 0;
  for (int i = 1; i <= N; i++) {
    int s, a, b;
    scanf("%d%d%d", &s, &a, &b);
    p.push_back({a - b, s});
    sum += 1ll * b * s;
    sums += s;
  }
  ans = sum;
  sum = 0;
  if (sums % S) {
    p.push_back({0, S - sums % S});
  }
  sort(p.begin(), p.end());
  long long r = 0;
  for (int i = p.size() - 1; i >= 0; i--) {
    if (r + p[i].second >= S) {
      d = max(d, (S - r) * p[i].first + sum);
      long long num = (r + p[i].second) / S;
      d = max(d, (num * S - r) * p[i].first + sum);
    }
    r = (r + p[i].second) % S;
    sum += 1ll * p[i].first * p[i].second;
  }
  ans += d;
  printf("%lld\n", ans);
  return 0;
}
