It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices.

It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?

Input

The first line of input will contain integers N and S (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively. N lines follow.

The i-th such line contains integers si, ai, and bi (1 ≤ si ≤ 105, 1 ≤ ai ≤ 105, 1 ≤ bi ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.

Output

Print the maximum total happiness that can be achieved.

Examples

Input

3 12
3 5 7
4 6 7
5 9 5


Output

84


Input

6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 9 6


Output

314

Note

In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct TnT {
  long long s, a, b;
} T[maxn];
long long TT[maxn];
bool cmp1(TnT x, TnT y) { return (x.a - x.b) < (y.a - y.b); }
bool cmp2(TnT x, TnT y) { return (x.b - x.a) < (y.b - y.a); }
int main() {
  long long N, S, pos, cnt, t1 = 0, t2 = 0;
  long long tmp, sum = 0, ans = 0;
  scanf("%lld %lld", &N, &S);
  for (long long i = 0; i < N; i++) {
    scanf("%lld %lld %lld", &T[i].s, &T[i].a, &T[i].b);
    if (T[i].a > T[i].b)
      t1 += T[i].s, sum += T[i].s * T[i].a;
    else
      t2 += T[i].s, sum += T[i].s * T[i].b;
  }
  t1 = t1 % S;
  t2 = t2 % S;
  if (t1 + t2 > S) {
    cout << sum << endl;
    return 0;
  }
  sort(T, T + N, cmp1);
  for (long long i = 0; i < N; i++) TT[i] = T[i].a - T[i].b;
  pos = upper_bound(TT, TT + N, 0) - TT;
  cnt = t1;
  tmp = sum;
  for (long long i = pos; i < N; i++) {
    if (cnt - T[i].s <= 0) {
      tmp -= cnt * TT[i];
      break;
    } else {
      tmp -= T[i].s * TT[i];
      cnt -= T[i].s;
    }
  }
  ans = max(ans, tmp);
  sort(T, T + N, cmp2);
  for (long long i = 0; i < N; i++) TT[i] = T[i].b - T[i].a;
  pos = lower_bound(TT, TT + N, 0) - TT;
  cnt = t2;
  tmp = sum;
  for (long long i = pos; i < N; i++) {
    if (cnt - T[i].s <= 0) {
      tmp -= cnt * TT[i];
      break;
    } else {
      tmp -= T[i].s * TT[i];
      cnt -= T[i].s;
    }
  }
  ans = max(ans, tmp);
  printf("%lld\n", ans);
  return 0;
}
