#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 1e5 + 5;
struct node {
  long long s, a, b, cha;
} pep[N];
bool cmp(node &u, node &v) { return u.cha > v.cha; }
long long n, s;
long long pznum;
long long ssum = 0;
long long ssum1 = 0, ssum2 = 0;
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> s;
  for (int i = 1; i <= n; ++i) {
    long long ss, a, b;
    cin >> ss >> a >> b;
    pep[i].s = ss;
    pep[i].a = a;
    pep[i].b = b;
    pep[i].cha = a - b;
    if (pep[i].cha > 0) {
      ssum1 += ss;
    } else if (pep[i].cha < 0) {
      ssum2 += ss;
    }
    ssum += ss;
  }
  pznum = (ssum + s - 1) / s;
  sort(pep + 1, pep + n + 1, cmp);
  long long ans = 0;
  ssum1 = ssum1 / s * s;
  ssum2 = ssum2 / s * s;
  long long sub = pznum - ssum1 / s - ssum2 / s;
  for (int i = 1; i <= n; ++i) {
    if (pep[i].cha > 0) {
      if (ssum1 >= pep[i].s) {
        ans += pep[i].s * pep[i].a;
        ssum1 -= pep[i].s;
        pep[i].s = 0;
      } else {
        ans += ssum1 * pep[i].a;
        pep[i].s -= ssum1;
        ssum1 = 0;
      }
    }
  }
  for (int i = n; i >= 1; --i) {
    if (pep[i].cha < 0) {
      if (ssum2 >= pep[i].s) {
        ans += pep[i].s * pep[i].b;
        ssum2 -= pep[i].s;
        pep[i].s = 0;
      } else {
        ans += ssum2 * pep[i].b;
        pep[i].s -= ssum2;
        ssum2 = 0;
      }
    }
  }
  if (sub >= 2) {
    for (int i = 1; i <= n; ++i) {
      ans += pep[i].s * max(pep[i].a, pep[i].b);
    }
  } else {
    long long tmp1 = 0, tmp2 = 0;
    for (int i = 1; i <= n; ++i) {
      tmp1 += pep[i].s * pep[i].a;
      tmp2 += pep[i].s * pep[i].b;
    }
    ans += max(tmp1, tmp2);
  }
  cout << ans << endl;
}
