#include <bits/stdc++.h>
using namespace std;
const int N1 = 100005;
const int inf = 0x3f3f3f3f;
int n, S;
int s[N1], a[N1], b[N1], p[N1], del[N1], tr[N1];
int cmp(int x, int y) { return del[x] < del[y]; }
long long cei(long long x, long long y) {
  if (x % y == 0)
    return x / y;
  else
    return x / y + 1;
}
int main() {
  scanf("%d%d", &n, &S);
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &s[i], &a[i], &b[i]);
  long long sum[2], ans = 0, tmp, ned;
  sum[0] = sum[1] = 0;
  for (int i = 1; i <= n; i++) {
    p[i] = (a[i] >= b[i]) ? 0 : 1;
    del[i] = abs(a[i] - b[i]);
    tr[i] = i;
    sum[p[i]] += s[i];
    ans += 1ll * s[i] * ((a[i] >= b[i]) ? a[i] : b[i]);
  }
  sort(tr + 1, tr + n + 1, cmp);
  if (cei(sum[0], S) + cei(sum[1], S) == cei(sum[0] + sum[1], S)) {
    printf("%lld\n", ans);
    return 0;
  }
  tmp = ans;
  ned = sum[0] % S;
  for (int j = 1; j <= n; j++) {
    int i = tr[j];
    if (p[i] == 0) {
      if (ned > s[i])
        tmp -= 1ll * s[i] * del[i], ned -= s[i];
      else {
        tmp -= 1ll * ned * del[i], ned = 0;
        break;
      }
    }
  }
  swap(ans, tmp);
  ned = sum[1] % S;
  for (int j = 1; j <= n; j++) {
    int i = tr[j];
    if (p[i] == 1) {
      if (ned > s[i])
        tmp -= 1ll * s[i] * del[i], ned -= s[i];
      else {
        tmp -= 1ll * ned * del[i], ned = 0;
        break;
      }
    }
  }
  ans = max(ans, tmp);
  printf("%lld\n", ans);
  return 0;
}
