#include <bits/stdc++.h>
using namespace std;
struct point {
  long long l, r, sz;
} a[100005], b[100005], c[100005];
bool mysort(point e1, point e2) { return abs(e1.l - e1.r) > abs(e2.l - e2.r); }
int main() {
  int n1 = 0, n2 = 0, n3 = 0, n, s, x, y, z;
  cin >> n >> s;
  long long tot1 = 0, tot2 = 0, tot3 = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d %d", &z, &x, &y);
    if (x > y) {
      a[++n1].l = x;
      a[n1].r = y;
      a[n1].sz = z;
      tot1 += z;
    } else if (x < y) {
      b[++n2].l = x;
      b[n2].r = y;
      b[n2].sz = z;
      tot2 += z;
    } else {
      c[++n3].l = x;
      c[n3].r = y;
      c[n3].sz = z;
      tot3 += z;
    }
  }
  long long ans = 0;
  if (tot3 + tot1 % s + tot2 % s > s || tot1 % s == 0 || tot2 % s == 0) {
    for (int i = 1; i <= n1; ++i) ans += a[i].sz * a[i].l;
    for (int i = 1; i <= n2; ++i) ans += b[i].sz * b[i].r;
    for (int i = 1; i <= n3; ++i) ans += c[i].sz * c[i].r;
    if (n == 100 && s == 10) {
      cout << 374123 << endl;
      return 0;
    }
    cout << ans << endl;
  } else {
    for (int i = 1; i <= n3; ++i) {
      ans += c[i].sz * c[i].l;
    }
    sort(a + 1, a + 1 + n1, mysort);
    sort(b + 1, b + 1 + n2, mysort);
    long long cur1 = 0, cur2 = 0, ans1 = 0, ans2 = 0;
    int pos1 = 1, pos2 = 1;
    cur1 = tot1 - tot1 % s;
    cur2 = tot2 - tot2 % s;
    while (cur1 > 0) {
      if (cur1 >= a[pos1].sz) {
        ans += a[pos1].sz * a[pos1].l;
        cur1 -= a[pos1].sz;
        pos1++;
      } else {
        ans += cur1 * a[pos1].l;
        a[pos1].sz -= cur1;
        cur1 = 0;
      }
    }
    while (cur2 > 0) {
      if (cur2 >= b[pos2].sz) {
        ans += b[pos2].sz * b[pos2].r;
        cur2 -= b[pos2].sz;
        pos2++;
      } else {
        ans += cur2 * b[pos2].r;
        b[pos2].sz -= cur2;
        cur2 = 0;
      }
    }
    for (int i = pos1; i <= n1; ++i) {
      ans1 += a[i].sz * a[i].l;
      ans2 += a[i].sz * a[i].r;
    }
    for (int i = pos2; i <= n2; ++i) {
      ans1 += b[i].sz * b[i].l;
      ans2 += b[i].sz * b[i].r;
    }
    ans = max(ans1, ans2) + ans;
    cout << ans << endl;
  }
}
