#include <bits/stdc++.h>
using namespace std;
const double pi = 3.1415926535897932384626433832795028841971;
long long fb[100100], fa[100100];
long long k[100100], a[100100], b[100100];
int main() {
  ios::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  long long n, s, sum = 0, ans = 0, ka = 0, kb = 0;
  cin >> n >> s;
  for (int i = 0; i < (n); i++) {
    cin >> k[i] >> a[i] >> b[i];
    if (a[i] >= b[i]) {
      fa[a[i] - b[i]] += k[i];
      ka += k[i];
    }
    if (a[i] <= b[i]) {
      fb[b[i] - a[i]] += k[i];
      kb += k[i];
    }
    sum += k[i];
    ans += k[i] * max(a[i], b[i]);
  }
  long long ks = (sum + s - 1) / s;
  if (((ka + s - 1) / s) + ((kb + s - 1) / s) == ks) {
    cout << ans;
    return 0;
  }
  kb %= s;
  ka %= s;
  long long sum1 = 0, sum2 = 0;
  for (long long i = 0; i < 100010; i++) {
    if (ka > 0) {
      sum1 += i * min(ka, fa[i]);
      ka -= min(ka, fa[i]);
    }
    if (kb > 0) {
      sum2 += i * min(kb, fb[i]);
      kb -= min(kb, fb[i]);
    }
  }
  cout << ans - min(sum1, sum2);
  return 0;
}
