Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other.

The entire universe turned into an enormous clock face with three hands — hour, minute, and second. Time froze, and clocks now show the time *h* hours, *m* minutes, *s* seconds.

Last time Misha talked with the coordinator at *t*<sub class="lower-index">1</sub> o'clock, so now he stands on the number *t*<sub class="lower-index">1</sub> on the clock face. The contest should be ready by *t*<sub class="lower-index">2</sub> o'clock. In the terms of paradox it means that Misha has to go to number *t*<sub class="lower-index">2</sub> somehow. Note that he doesn't have to move forward only: in these circumstances time has no direction.

Clock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way).

Given the hands' positions, *t*<sub class="lower-index">1</sub>, and *t*<sub class="lower-index">2</sub>, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from *t*<sub class="lower-index">1</sub> to *t*<sub class="lower-index">2</sub> by the clock face.
Five integers *h*, *m*, *s*, *t*<sub class="lower-index">1</sub>, *t*<sub class="lower-index">2</sub> (1 ≤ *h* ≤ 12, 0 ≤ *m*, *s* ≤ 59, 1 ≤ *t*<sub class="lower-index">1</sub>, *t*<sub class="lower-index">2</sub> ≤ 12, *t*<sub class="lower-index">1</sub> ≠ *t*<sub class="lower-index">2</sub>).

Misha's position and the target time do not coincide with the position of any hand.
The three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same.
12 30 45 3 11
NO
12 0 1 12 1
YES
3 47 0 4 9
YES
10 22 59 6 10
YES
3 1 13 12 3
NO
11 19 28 9 10
YES
9 38 22 6 1
NO
5 41 11 5 8
NO
11 2 53 10 4
YES
9 41 17 10 1
YES
6 54 48 12 6
YES
12 55 9 5 1
NO
8 55 35 9 3
NO
3 21 34 3 10
YES
2 52 1 12 3
NO
7 17 11 1 7
NO
11 6 37 6 4
YES
9 6 22 8 1
NO
3 10 5 5 9
YES
7 12 22 11 2
YES
7 19 4 7 3
NO
11 36 21 4 6
NO
10 32 49 1 3
YES
1 9 43 11 3
NO
1 8 33 4 8
NO
3 0 33 9 4
NO
7 15 9 10 3
NO
8 3 57 11 1
NO
1 33 49 5 9
NO
3 40 0 5 7
YES
5 50 9 2 7
NO
10 0 52 6 1
YES
3 10 4 1 11
NO
2 41 53 4 6
YES
10 29 30 4 7
NO
5 13 54 9 11
NO
1 0 23 3 9
NO
1 0 41 12 1
NO
6 30 30 3 9
YES
3 7 32 11 10
YES
1 0 25 12 4
NO
12 0 0 5 6
YES
1 5 4 3 2
YES
6 30 30 9 10
YES
6 0 0 2 8
NO
10 50 59 9 10
YES
12 59 59 12 6
NO
3 0 30 3 4
NO
2 10 10 1 11
YES
10 5 30 1 12
YES
5 29 31 5 10
YES
5 2 2 11 2
NO
5 15 46 3 10
YES
1 30 50 1 2
NO
5 26 14 1 12
YES
1 58 43 12 1
YES
12 0 12 11 1
NO
6 52 41 6 5
YES
5 8 2 1 3
NO
2 0 0 1 3
NO
1 5 6 2 1
YES
9 5 5 11 12
YES
12 5 19 3 4
NO
6 14 59 1 3
NO
10 38 34 4 12
YES
2 54 14 2 12
YES
5 31 0 6 7
NO
6 15 30 3 9
YES
3 54 41 8 10
NO
3 39 10 10 12
YES
1 11 50 1 2
NO
5 40 24 8 1
NO
9 5 59 1 3
NO
5 0 0 6 7
YES
4 40 59 6 8
YES
10 13 55 12 1
YES
6 50 0 5 6
YES
7 59 3 7 4
YES
6 0 1 6 7
NO
6 15 55 3 5
NO
12 9 55 10 2
YES
2 0 1 11 2
NO
8 45 17 12 9
NO
5 30 31 11 3
YES
6 43 0 10 6
NO
6 30 30 1 11
YES
11 59 59 11 12
YES
5 45 35 9 5
NO
2 43 4 9 7
NO
12 30 50 6 9
NO
1 10 1 2 3
NO
10 5 55 9 1
NO
1 59 59 2 3
YES
1 49 14 10 3
NO
3 15 15 2 4
YES
10 5 55 1 5
NO
6 33 45 12 6
YES
1 20 20 11 1
YES
2 30 45 1 11
YES
1 55 1 11 10
YES
3 0 1 11 1
NO
1 5 6 1 12
YES
12 10 5 11 4
YES
6 5 59 12 1
YES
12 0 20 11 12
YES
3 25 30 4 5
YES
2 15 18 11 1
YES
12 5 48 11 9
NO
6 30 30 10 2
YES
1 0 11 1 2
NO
10 0 1 10 11
NO
3 30 45 10 1
YES
11 21 56 3 12
YES
3 16 16 3 4
YES
11 55 55 1 12
YES
12 12 12 11 12
YES
3 5 1 1 2
NO
3 30 45 10 11
YES
10 0 1 9 10
YES
3 0 5 3 6
NO
1 1 49 1 10
NO
9 45 15 11 12
YES
12 15 59 3 8
NO
5 0 5 3 6
NO
3 30 45 1 2
YES
2 10 11 2 4
YES
2 10 10 4 3
YES
3 30 59 1 2
YES
11 59 59 12 11
YES
10 25 50 5 6
NO
6 30 30 9 3
YES
8 14 40 12 3
NO
2 10 10 1 6
YES
# -*- coding: utf-8 -*-

import math
import collections
import bisect
import heapq
import time
import random
"""
created by shhuan at 2017/10/3 12:35

C. Ordering Pizza
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.



先计算出需要买的披萨个数，等于所有人要吃的片数之和除以每个披萨的片数。
如果某个人吃第一种披萨幸福值更高，就尽量让他吃第一种，否则吃第二种。
如果第一种披萨不能满足所有更喜欢吃第一种披萨的人，那么优先满足吃第一种比吃第二种差值更大的人，所有先对差值排序。


"""

N, S = map(int, input().split())

M = []
for i in range(N):
    M.append([int(x) for x in input().split()])
#
# N, S = 100000, 100000
# M = [[random.randint(10000, 100000), random.randint(1, 100), random.randint(1, 100)] for _ in range(N)]
# t0 = time.time()

s = [M[i][0] for i in range(N)]
a = [M[i][1] for i in range(N)]
b = [M[i][2] for i in range(N)]

total = sum(s)
numpizza = int(math.ceil(total/S))
numslice = numpizza * S

sab = sorted([(a[i]-b[i], i) for i in range(N)], reverse=True)

pa = 0
pb = 0
pab = 0
maxHappiness = 0
for i in range(N):
    if a[i] > b[i]:
        pa += s[i]
    elif a[i] < b[i]:
        pb += s[i]
    else:
        pab += s[i]

sa = int(math.ceil(pa/S)) * S
sb = int(math.ceil(pb/S)) * S
pab -= sa+sb-pa-pb
pab = max(pab, 0)
if sa//S + sb//S + int(math.ceil(pab/S)) == numpizza:
    print(sum([s[i]*(a[i] if a[i] > b[i] else b[i]) for i in range(N)]))
    exit(0)



if pa == 0 and pb == 0:
    print(sum([s[i]*a[i] for i in range(N)] or [0]))
    exit(0)
if pa == 0:
    print(sum([s[i]*b[i] for i in range(N)] or [0]))
    exit(0)
if pb == 0:
    print(sum([s[i]*a[i] for i in range(N)] or [0]))
    exit(0)

# print(time.time()-t0)
sbak = s
for pza in range(pa//S, (pa+pab)//S+2):
    pzb = numpizza - pza

    aslice = pza*S
    bslice = pzb*S

    h = 0
    s = [x for x in sbak]
    for j in range(N):
        d, i = sab[j]
        if d > 0:
            if aslice >= s[i]:
                h += s[i]*a[i]
                aslice -= s[i]
                s[i] = 0
            elif aslice > 0:
                h += a[i]*aslice
                s[i] -= aslice
                aslice = 0
        else:
            break
    for j in range(N-1, -1, -1):
        d, i = sab[j]
        if d < 0:
            if bslice >= s[i]:
                h += s[i]*b[i]
                bslice -= s[i]
                s[i] = 0
            elif bslice > 0:
                h += bslice * b[i]
                s[i] -= bslice
                bslice = 0
        else:
            break

    if aslice == 0:
        for i in range(N):
            if s[i] != 0:
                h += s[i]*b[i]
                bslice -= s[i]
    elif bslice == 0:
        for i in range(N):
            if s[i] != 0:
                h += s[i]*a[i]
                aslice -= s[i]
    else:
        remain = [(s[i], a[i], b[i]) for i in range(N) if s[i] > 0 and a[i] != b[i]]
        for u,v,w in remain:
            if v > w:
                if aslice > 0:
                    if aslice > u:
                        h += u * v
                        aslice -= u
                    else:
                        h += aslice * v
                        h += (u-aslice) * w
                        bslice -= u-aslice
                        aslice = 0
                else:
                    h += u*w
                    bslice -= u

            else:
                if bslice > 0:
                    if bslice > u:
                        h += u * w
                        bslice -= u
                    else:
                        h += bslice * w
                        h += (u - bslice) * v
                        aslice -= u - bslice
                        bslice = 0
                else:
                    h += u*v
                    aslice -= u
        h += sum([s[i]*a[i] for i in range(N) if a[i] == b[i]] or [0])

    maxHappiness = max(maxHappiness, h)

print(maxHappiness)

# print(time.time() - t0 )