#include <bits/stdc++.h>
using namespace std;
struct Contestant {
  int s, a, b;
  bool operator<(const Contestant &other) const {
    return b - a > other.b - other.a;
  }
};
const int MAXN = 100005;
Contestant ca[MAXN];
long long ori, cur, ans, sum, possum;
int N, S, rem;
void Check(long long res) {
  long long cur = ori;
  bool mf = true;
  for (int i = 0; i < N && res > 0; ++i) {
    if (ca[i].b - ca[i].a < 0 && mf) {
      res -= min(res, 1LL * rem);
      mf = false;
    }
    int dt = min(1LL * ca[i].s, res);
    cur += 1LL * dt * (ca[i].b - ca[i].a);
    res -= dt;
  }
  ans = max(ans, cur);
  return;
}
int main() {
  scanf("%d%d", &N, &S);
  for (int i = 0; i < N; ++i)
    scanf("%d%d%d", &ca[i].s, &ca[i].a, &ca[i].b),
        ori += 1LL * ca[i].s * ca[i].a, sum += ca[i].s,
        possum += ca[i].b >= ca[i].a ? ca[i].s : 0;
  rem = sum % S == 0 ? 0 : S - sum % S;
  sort(ca, ca + N);
  Check(possum / S * S);
  Check((possum + S - 1) / S * S);
  printf("%I64d\n", ans);
  return 0;
}
