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 org.omg.CORBA.FREE_MEM;

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Solution {


    static MyScanner sc;
    private static PrintWriter out;
    static long M = 1000000007;


    public static void main(String[] s) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
        //        stringBuilder.append("");

        if (stringBuilder.length() == 0) {
            sc = new MyScanner(System.in);
        } else {
            sc = new MyScanner(new BufferedReader(new StringReader(stringBuilder.toString())));
        }

        out = new PrintWriter(new OutputStreamWriter(System.out));

        initData();
        solve();
        out.flush();
    }

    private static void initData() {
    }


    private static void solve() {
        int n = sc.nextInt();
        int k = sc.nextInt();
        long hap = 0;
        ArrayList<int[]> p1 = new ArrayList<>();
        ArrayList<int[]> p2 = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int[] l = sc.na(3);
            if (l[1] > l[2]) {
                hap += 1L * l[2] * l[0];
                p1.add(new int[]{l[0], l[1] - l[2]});
            } else {
                hap += 1L * l[1] * l[0];
                p2.add(new int[]{l[0], l[2] - l[1]});
            }
        }
        p1.sort((t1, t2) -> t2[1] - t1[1]);
        p2.sort((t1, t2) -> t2[1] - t1[1]);


        long[] sum1 = new long[p1.size() + 1];
        long[] sum2 = new long[p2.size() + 1];

        for (int i = 0; i < p1.size(); i++) {
            sum1[i + 1] = sum1[i] + p1.get(i)[0];
        }
        for (int i = 0; i < p2.size(); i++) {
            sum2[i + 1] = sum2[i] + p2.get(i)[0];
        }
        long mp1 = sum1[p1.size()] / k;
        long mp2 = sum2[p2.size()] / k;
        long sl1 = mp1 * k;
        long sl2 = mp2 * k;
        long ts1 = sum1[p1.size()];
        long ts2 = sum2[p2.size()];

        ts1 -= sl1;
        ts2 -= sl2;

        int k11 = 0;
        for (; k11 < p1.size(); k11++) {
            if (p1.get(k11)[0] > sl1) {
                hap += 1L * sl1 * p1.get(k11)[1];
                p1.get(k11)[0] -= sl1;
                break;
            }
            hap += 1L * p1.get(k11)[0] * p1.get(k11)[1];
            sl1 -= p1.get(k11)[0];
            p1.get(k11)[0] = 0;
        }

        int k21 = 0;
        for (; k21 < p2.size(); k21++) {
            if (p2.get(k21)[0] > sl2) {
                hap += 1L *sl2 * p2.get(k21)[1];
                p2.get(k21)[0] -= sl2;
                break;
            }
            hap += 1L *p2.get(k21)[0] * p2.get(k21)[1];
            sl2 -= p2.get(k21)[0];
            p2.get(k21)[0] = 0;
        }
        long t1 = 0;
        long t2 = 0;
        if (ts1 + ts2 > k) {
            for (int i = k11; i < p1.size(); i++) {
                hap += 1L *p1.get(i)[0] * p1.get(i)[1];
            }

            for (int i = k21; i < p2.size(); i++) {
                hap += 1L *p2.get(i)[0] * p2.get(i)[1];
            }

        } else {
            for (int i = k11; i < p1.size(); i++) {
                t1 += 1L *p1.get(i)[0] * p1.get(i)[1];
            }

            for (int i = k21; i < p2.size(); i++) {
                t2 += 1L *p2.get(i)[0] * p2.get(i)[1];
            }
        }


        out.println(hap + Math.max(t1, t2));
    }

    private static int res(int i1, int i) {
        if (i == 1) return 1;
        if (i1 == 0) return 1;
        int sum = 0;
        for (int m = 0; m <= i1 / i; m++) {
            sum += res(i1 - m * i, i / 2);
        }
        return sum;
    }


    private static void printa(int[] res) {
        for (int i : res) {
            out.print(i);
            out.print(' ');
        }
    }

    private static void solveT() {
        int t = sc.nextInt();
        while (t-- > 0) {
            solve();
        }
    }


    private static int gcd(int l, int l1) {
        if (l > l1) return gcd(l1, l);
        if (l == 0) return l1;
        return gcd(l1 % l, l);
    }

    private static long pow(long a, long b, long m) {
        if (b == 0) return 1;
        if (b == 1) return a;
        long pp = pow(a, b / 2, m);
        pp *= pp;
        pp %= m;
        return (pp * (b % 2 == 0 ? 1 : a)) % m;
    }

    private static void sort(int[] t, IntUnaryOperator op1, IntUnaryOperator op2) {
        sort(t, (k1, k2) -> {
            int p1 = op1.applyAsInt(k1);
            int p2 = op1.applyAsInt(k2);
            if (p1 != p2) {
                return p1 - p2;
            } else {
                return op2.applyAsInt(k1) - op2.applyAsInt(k2);
            }
        });
    }

    private static void sort(int[] t, IntBinaryOperator op) {
        List<Integer> k = Arrays.stream(t).boxed().collect(Collectors.toList());
        k.sort(op::applyAsInt);
        for (int i = 0; i < t.length; i++) {
            t[i] = k.get(i);
        }
    }

    static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        MyScanner(BufferedReader br) {
            this.br = br;
        }

        public MyScanner(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

        void findToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        String next() {
            findToken();
            return st.nextToken();
        }

        int[] na(int n) {
            int[] k = new int[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.nextInt();
            }
            return k;
        }

        long[] nl(int n) {
            long[] k = new long[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.nextLong();
            }
            return k;
        }

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

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

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}