#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > v1, v2;
int N, S;
long long s, a, b;
long long ans, use1, use2;
int main() {
  scanf("%d%d", &N, &S);
  v1.clear();
  v2.clear();
  for (int i = 1; i <= N; i++) {
    scanf("%lld%lld%lld", &s, &a, &b);
    if (a < b) {
      ans += s * b;
      use2 = (use2 + s) % S;
      v2.push_back(make_pair(b - a, s));
    } else {
      ans += s * a;
      use1 = (use1 + s) % S;
      v1.push_back(make_pair(a - b, s));
    }
  }
  if (use1 + use2 > S) {
    printf("%lld\n", ans);
    return 0;
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  long long a1 = 0, a2 = 0;
  for (vector<pair<long long, long long> >::iterator it = v1.begin();
       it != v1.end(); it++) {
    a1 += min(use1, it->second) * (it->first);
    use1 -= min(use1, it->second);
  }
  for (vector<pair<long long, long long> >::iterator it = v2.begin();
       it != v2.end(); it++) {
    a2 += min(use2, it->second) * (it->first);
    use2 -= min(use2, it->second);
  }
  ans -= min(a1, a2);
  printf("%lld\n", ans);
  return 0;
}
