#include <bits/stdc++.h>
using namespace std;
struct st {
  long long s, a, b;
} p[100005];
bool cmp(st a, st b) { return abs(a.a - a.b) > abs(b.a - b.b); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, ss;
  cin >> n >> ss;
  long long sum = 0, cnt = 0;
  ;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].s >> p[i].a >> p[i].b;
    sum += p[i].s;
  }
  cnt = sum / ss;
  if (sum % ss != 0) cnt++;
  long long number1 = 0, number2 = 0, cnt1 = 0, cnt2 = 0;
  sort(p + 1, p + 1 + n, cmp);
  long long result = 0;
  for (int i = 1; i <= n; i++) {
    if (p[i].a > p[i].b) {
      number1 += p[i].s;
      result += p[i].s * p[i].a;
    } else {
      number2 += p[i].s;
      result += p[i].s * p[i].b;
    }
  }
  cnt1 = number1 / ss;
  if (number1 % ss != 0) cnt1++;
  cnt2 = number2 / ss;
  if (number2 % ss != 0) cnt2++;
  if (cnt2 + cnt1 <= cnt) {
    cout << result << endl;
  } else {
    long long d1 = number1 % ss;
    long long result1 = result;
    for (int i = n; i >= 1; i--) {
      if (p[i].a > p[i].b) {
        if (d1 >= p[i].s) {
          result1 = result1 - p[i].s * (p[i].a - p[i].b);
          d1 -= p[i].s;
        } else {
          result1 = result1 - d1 * (p[i].a - p[i].b);
          d1 = 0;
        }
      }
      if (d1 == 0) break;
    }
    d1 = number2 % ss;
    for (int i = n; i >= 1; i--) {
      if (p[i].a <= p[i].b) {
        if (d1 >= p[i].s) {
          result = result - p[i].s * (p[i].b - p[i].a);
          d1 -= p[i].s;
        } else {
          result = result - d1 * (p[i].b - p[i].a);
          d1 = 0;
        }
      }
      if (d1 == 0) break;
    }
    result = max(result, result1);
    cout << result << endl;
  }
  return 0;
}
