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.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        new Main().run(in, out);
        out.close();
    }


    int S;
    void run(FastScanner in, PrintWriter out) {

        N = in.nextInt();
        S = in.nextInt();

        totalSlices = 0;
        p = new long[N][3];
        for (int i = 0; i < N; i++) {
            p[i][0] = in.nextInt();
            p[i][1] = in.nextInt();
            p[i][2] = in.nextInt();
            totalSlices += p[i][0];
        }

        totalPizzas = (long)Math.ceil((double)totalSlices/S);
        Arrays.sort(p, (a, b) -> Long.compare(a[2]-a[1], b[2]-b[1]));
        // Arrays.sort(p, (a, b) -> {
        //     if (a[1] >= a[2] && b[1] >= b[2]) {
        //         return Long.compare(b[1], a[1]);
        //     }
        //     if (a[1] <= a[2] && b[1] <= b[2]) {
        //         return Long.compare(a[2], b[2]);
        //     }

        //     if (a[1] > a[2] && b[1] < b[2]) {
        //         return -1;
        //     }

        //     return 1;
        // });


        long lo = 0;
        long hi = totalPizzas+1;

        while (hi-lo>3) {
            long m1 = lo+(hi-lo)/3;
            long m2 = hi-(hi-lo)/3;
            long score1 = score(m1);
            long score2 = score(m2);

            if (score1 == score2) {
                lo = m1;
                hi = m2;
            } else if (score1 < score2) {
                lo = m1;
            } else {
                hi = m2;
            }
        }

        long max = 0;
        for (long i = lo; i <= hi; i++) {
            max = Math.max(max, score(i));
        }

        // for (int i = 0; i <= totalPizzas; i++) {
        //     System.out.println(i + " " +score(i));
        // }
        out.println(max);

    }

    int N;
    long[][] p;
    long totalSlices;
    long totalPizzas;
    long score(long a) {

        long aslices = 0;
        long bslices = 0;
        long apizzas = a;
        long bpizzas = totalPizzas-a;

        long ascore = 0;
        long bscore = 0;

        int l = 0;
        int r = N-1;
        while (l <= r) {

            if (preferA(l)) {
                if (aslices+p[l][0] <= apizzas*S) {
                    aslices += p[l][0];
                    ascore += p[l][0]*p[l][1];
                    l++;
                } else {
                    long budget = apizzas*S - aslices;
                    long atake = budget;
                    long btake = p[l][0] - budget;
                    ascore += p[l][1] * atake;
                    bscore += p[l][2] * btake;
                    l++;

                    aslices += atake;
                    bslices += btake;
                }
            } else {
                if (bslices+p[r][0] <= bpizzas*S) {
                    bslices += p[r][0];
                    bscore += p[r][0]*p[r][2];
                    r--;
                } else {
                    long budget = bpizzas*S - bslices;
                    long btake = budget;
                    long atake = p[r][0] - budget;
                    ascore += p[r][1] * atake;
                    bscore += p[r][2] * btake;
                    r--;

                    aslices += atake;
                    bslices += btake;
                }

            }
        }

        return bscore + ascore;

    }

    boolean preferA(int i) {
        return p[i][1] >= p[i][2];
    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
            st = null;
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}
