#include <bits/stdc++.h>
using namespace std;
struct person {
  long long k, a, b;
  person(int x = 0, int y = 0, int z = 0) : k(x), a(y), b(z) {}
};
bool comp(person x, person y) { return abs(x.a - x.b) < abs(y.a - y.b); }
int n;
long long s;
vector<person> p;
long long get_ans1(long long ans, long long d) {
  int i = 0;
  while ((d > 0) && (i < n)) {
    if (p[i].a <= p[i].b) {
      i++;
      continue;
    }
    ans -= min(d, p[i].k) * p[i].a;
    ans += min(d, p[i].k) * p[i].b;
    d -= min(d, p[i].k);
    i++;
  }
  return ans;
}
long long get_ans2(long long ans, long long d) {
  int i = 0;
  while ((d > 0) && (i < n)) {
    if (p[i].b < p[i].a) {
      i++;
      continue;
    }
    ans -= min(d, p[i].k) * p[i].b;
    ans += min(d, p[i].k) * p[i].a;
    d -= min(d, p[i].k);
    i++;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  p.resize(n);
  for (int i = 0; i < n; i++) cin >> p[i].k >> p[i].a >> p[i].b;
  sort(p.begin(), p.end(), comp);
  long long cur1 = 0, cur2 = 0, ans = 0, all = 0;
  for (int i = 0; i < n; i++) {
    if (p[i].a > p[i].b)
      cur1 += p[i].k, ans += p[i].k * p[i].a;
    else
      cur2 += p[i].k, ans += p[i].k * p[i].b;
    all += p[i].k;
  }
  long long k0 = (all - 1) / s + 1;
  long long k1 = (cur1 - 1) / s + 1;
  long long k2 = (cur2 - 1) / s + 1;
  if (k0 == k1 + k2) {
    cout << ans << "\n";
    return 0;
  }
  long long d1 = cur1 - (cur1 / s) * s, d2 = cur2 - (cur2 / s) * s;
  cout << max(get_ans1(ans, d1), get_ans2(ans, d2));
  return 0;
}
