#include <bits/stdc++.h>
using namespace std;
long long inf = 2147483647;
const long long ll = 9223372036854775807, ninf = 1000000000;
const double eps = 1e-6;
const long long nll = 1000000000000000000;
int main() {
  long long a, b;
  while (cin >> a >> b) {
    long long nb = 0, nc = 0;
    long long ans = 0;
    vector<pair<long long, long long> > A, B;
    for (int i = 0; i < a; i++) {
      long long x, y, z;
      cin >> x >> y >> z;
      if (y > z) {
        ans += x * y;
        pair<long long, long long> tmp;
        tmp.first = y - z;
        tmp.second = x;
        nb += x;
        A.push_back(tmp);
      } else {
        ans += x * z;
        pair<long long, long long> tmp;
        tmp.first = z - y;
        tmp.second = x;
        nc += x;
        B.push_back(tmp);
      }
    }
    nb %= b;
    nc %= b;
    if (nb + nc > b) {
      cout << ans << endl;
      continue;
    }
    long long ta = 0, tb = 0;
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    for (int i = 0; nb; i++) {
      ta += min(nb, A[i].second) * A[i].first;
      nb -= min(nb, A[i].second);
    }
    for (int i = 0; nc; i++) {
      tb += min(nc, B[i].second) * B[i].first;
      nc -= min(nc, B[i].second);
    }
    long long num = min(tb, ta);
    ans -= num;
    cout << ans << endl;
  }
  return 0;
}
