#include <bits/stdc++.h>
using namespace std;
class _pizzaForLife {
 public:
  long long dif;

 public:
  long long count;
};
bool acompare(_pizzaForLife n1, _pizzaForLife n2) { return n1.dif < n2.dif; }
_pizzaForLife client1[100001];
_pizzaForLife client2[100001];
long long n, s;
long long c, h1, h2;
long long c1, c2;
long long p1, p2;
long long ans, ans1, ans2;
void ILovePizza(long long count, long long happy1, long long happy2) {
  if (happy1 > happy2) {
    client1[++p1].count = count;
    client1[p1].dif = happy1 - happy2;
    c1 += count;
    ans += count * happy1;
  } else {
    client2[++p2].count = count;
    client2[p2].dif = happy2 - happy1;
    c2 += count;
    ans += count * happy2;
  }
}
int main() {
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> c >> h1 >> h2;
    ILovePizza(c, h1, h2);
  }
  c1 %= s;
  c2 %= s;
  if (c1 + c2 > s) {
    cout << ans << endl;
    return 0;
  }
  sort(client1 + 1, client1 + p1 + 1, acompare);
  sort(client2 + 1, client2 + p2 + 1, acompare);
  for (int i = 1; c1; i++) {
    ans1 += min(c1, client1[i].count) * client1[i].dif;
    c1 -= min(c1, client1[i].count);
  }
  for (int i = 1; c2; i++) {
    ans2 += min(c2, client2[i].count) * client2[i].dif;
    c2 -= min(c2, client2[i].count);
  }
  ans -= min(ans1, ans2);
  cout << ans << endl;
}
