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.
import java.util.*;
import java.io.*;
import java.math.*;
import java.util.regex.*;

public class Main {

    static Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    static Reader fastInput = new Reader();

    public static void main(String[] args) throws IOException {
        int N = fastInput.nextInt();
        int S = fastInput.nextInt();

        PriorityQueue<trip> Aq = new PriorityQueue<trip>((trip f, trip s) -> (f.a - f.b) - (s.a - s.b));
        PriorityQueue<trip> Bq = new PriorityQueue<trip>((trip f, trip s) -> (f.b - f.a) - (s.b - s.a));
        ArrayList<trip> m = new ArrayList<trip>();
        long aSum = 0, bSum = 0, mSum = 0;
        for (int i = 0; i < N; i++) {
            int s = fastInput.nextInt(), a = fastInput.nextInt(), b = fastInput.nextInt();
            if (a > b) {
                Aq.offer(new trip(s, a, b));
                aSum += s;
            } else if (a < b) {
                Bq.offer(new trip(s, a, b));
                bSum += s;
            } else {
                m.add(new trip(s, a, b));
                mSum += s;
            }
        }
        if ((mSum + aSum + bSum) % S != 0) {
            m.add(new trip((int) ((S - ((mSum + aSum + bSum) % S)) % S), 0, 0));
            mSum += (S - ((mSum + aSum + bSum) % S)) % S;
        }
        if (((S - (aSum % S)) % S) > mSum) {
            while (aSum % S != 0) {
                trip cur = Aq.poll();
                if (cur.s <= aSum % S) {
                    m.add(cur);
                    aSum -= cur.s;
                } else {
                    m.add(new trip((int) (aSum % S), cur.a, cur.b));
                    Aq.offer(new trip((int) (cur.s - (aSum % S)), cur.a, cur.b));
                    aSum -= aSum % S;
                }
            }
            while (bSum % S != 0) {
                trip cur = Bq.poll();
                if (cur.s <= bSum % S) {
                    m.add(cur);
                    bSum -= cur.s;
                } else {
                    m.add(new trip((int) (bSum % S), cur.a, cur.b));
                    Bq.offer(new trip((int) (cur.s - (bSum % S)), cur.a, cur.b));
                    bSum -= bSum % S;
                }
            }
        }
        long amHSum = 0, bmHSum = 0, aHSum = 0, bHSum = 0;
        for (trip cur : Aq) {
            aHSum += (long) cur.s * (long) cur.a;
        }
        for (trip cur : Bq) {
            bHSum += (long) cur.s * (long) cur.b;
        }
        for (trip cur : m) {
            amHSum += (long) cur.s * (long) cur.a;
        }
        for (trip cur : m) {
            bmHSum += (long) cur.s * (long) cur.b;
        }
        System.out.println(aHSum + Math.max(amHSum, bmHSum) + bHSum);
    }

    static class trip {

        int s, a, b;

        trip(int s1, int a1, int b1) {
            s = s1;
            a = a1;
            b = b1;
        }
    }

    static class Reader {

        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') {
                    break;
                }
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            boolean neg = (c == '-');
            if (neg) {
                c = read();
            }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg) {
                return -ret;
            }
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            boolean neg = (c == '-');
            if (neg) {
                c = read();
            }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) {
                return -ret;
            }
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            boolean neg = (c == '-');
            if (neg) {
                c = read();
            }

            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg) {
                return -ret;
            }
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) {
                buffer[0] = -1;
            }
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) {
                fillBuffer();
            }
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null) {
                return;
            }
            din.close();
        }
    }
}