#include <bits/stdc++.h>
using namespace std;
long long n, m, cntall, cur;
long long s[100010], a[100010], b[100010];
long long ans, ansadd;
pair<long long, long long> t[100010];
long long work(long long req, bool allowexit) {
  long long ans = 0, tmp;
  while (req) {
    if (t[cur].first > 0 && t[cur].second > req) {
      tmp = (t[cur].second - req) / m * m;
      ans += tmp * t[cur].first;
      t[cur].second -= tmp;
    }
    if (allowexit && t[cur].first < 0) return ans;
    tmp = min(t[cur].second, req);
    ans += tmp * t[cur].first;
    t[cur].second -= tmp;
    req -= tmp;
    if (t[cur].second == 0) cur--;
    if (cur < 0) return ans;
  }
  return ans;
}
int main() {
  long long i, tmp;
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < n; i++) scanf("%lld%lld%lld", &s[i], &a[i], &b[i]);
  for (i = 0; i < n; i++) {
    t[i].first = b[i] - a[i];
    t[i].second = s[i];
    ans += s[i] * a[i];
    cntall += s[i];
  }
  sort(t, t + n);
  cur = n - 1;
  if (cntall % m == 0) {
    tmp = work(m, 0);
    while (tmp > 0) {
      ans += tmp;
      if (cur < 0) break;
      tmp = work(m, 0);
    }
  } else {
    while (1) {
      if (cur < 0) break;
      tmp = work(cntall % m, 0);
      if (tmp > 0)
        ans += tmp;
      else
        break;
      if (cur < 0) break;
      tmp = work(m - cntall % m, 1);
      ans += tmp;
      if (t[cur].first < 0) break;
    }
  }
  printf("%lld", ans);
}
