import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;

import static java.lang.Math.*;

public class ProblemB9 {
    BufferedReader rd;

    ProblemB9() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] u = intarr();
        int n = u[0];
        long s = u[1];

        long[][] p = new long[n][];
        for(int i=0;i<n;i++) {
            p[i] = longarr();
        }
        out(solve(s,p));
    }

    private long solve(long s, long[][] p) {
        Map<Pair, Long> m = new HashMap<>();
        for(long[] u: p) {
            Pair pair = new Pair(u[1], u[2]);
            Long cur = m.get(pair);
            if(cur == null) {
                m.put(pair, u[0]);
            } else {
                m.put(pair, cur + u[0]);
            }
        }
        long[][] p2 = new long[m.size()][3];
        int i = 0;
        for(Map.Entry<Pair, Long> e: m.entrySet()) {
            p2[i][0] = e.getValue();
            p2[i][1] = e.getKey().x;
            p2[i][2] = e.getKey().y;
            i++;
        }
        return new Solver(s, p2).solve();
    }

    class Pair {
        long x,y;

        public Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            return y == pair.y;
        }

        @Override
        public int hashCode() {
            int result = (int) (x ^ (x >>> 32));
            result = 31 * result + (int) (y ^ (y >>> 32));
            return result;
        }
    }

    private class Solver {
        final long s;
        final int n;
        Map<Long, Long> ha = new HashMap<>();
        long[][] p;
        long sum, cnt;
        boolean[] same;
        int[] sortedA;
        int[] sortedB;

        private long cm(int x, int k) {
            return ((abs(p[x][k]-p[x][3-k]))<<40) + ((p[x][k] << 20) + 1000000 - p[x][3-k]);
        }

        private Solver(long s, long[][] p) {
            this.s = s;
            n = p.length;
            this.p = p;
            for(int i=0;i<n;i++) {
                sum += p[i][0];
            }
            cnt = sum / s;
            if(sum % s > 0) {
                cnt++;
            }
            same = new boolean[n];
            for(int i=0;i<n;i++) {
                same[i] = p[i][1] == p[i][2];
            }
            long[] wa = new long[n];
            long[] wb = new long[n];
            for(int i=0;i<n;i++) {
                wa[i] = cm(i,1);
                wb[i] = cm(i,2);
            }
            NavigableSet<Integer> byA = new TreeSet<>((x,y) -> Long.compare(wa[y], wa[x]));
            NavigableSet<Integer> byB = new TreeSet<>((x,y) -> Long.compare(wb[y], wb[x]));
            for(int i=0;i<n;i++) {
                byA.add(i);
                byB.add(i);
            }
            sortedA = new int[n];
            sortedB = new int[n];
            Iterator<Integer> itA = byA.iterator();
            Iterator<Integer> itB = byB.iterator();
            for(int i=0;i<n;i++) {
                sortedA[i] = itA.next();
                sortedB[i] = itB.next();
            }
        }

        private long solve() {
            long a0 = happiness(0);
            long an = happiness(cnt);
            long ma = max(a0,an);
            long left = 0;
            long right = cnt;
            while(right - left >= 3) {
                long step = (right - left) / 3;
                long mid1 = left + step;
                long mid2 = mid1 + step;
                long amid1 = happiness(mid1);
                long amid2 = happiness(mid2);
                if(amid1 > amid2) {
                    right = mid2;
                } else {
                    left = mid1;
                }
            }
            long i = left;
            while(i <= left+5 && i <= cnt) {
                ma = max(ma, happiness(i));
                i++;
            }
            return ma;
        }

        private long happiness(long aCnt) {
            Long res = ha.get(aCnt);
            if(res == null) {
                res = computeHappiness(aCnt);
                ha.put(aCnt, res);
            }
            return res;
        }

        private long computeHappiness(long aCnt) {
            int itA = 0;
            int itB = 0;
            long allA = aCnt*s;
            long allB = (cnt-aCnt)*s;
            long[] eaten = new long[n];
            int ua = sortedA[itA++];
            int ub = sortedB[itB++];
            long ha = 0;
            while(ua >= 0 || ub >= 0) {
                if(ua >= 0 && ((eaten[ua] >= p[ua][0]) || same[ua])) {
                    if(same[ua] && eaten[ua] < p[ua][0]) {
                        ha += p[ua][1] * p[ua][0];
                        eaten[ua] = p[ua][0];
                    }
                    if(itA < n) {
                        ua = sortedA[itA++];
                    } else {
                        ua = -1;
                    }
                } else if(ub >= 0 && ((eaten[ub] >= p[ub][0]) || same[ub])) {
                    if(same[ub] && eaten[ub] < p[ub][0]) {
                        ha += p[ub][2] * p[ub][0];
                        eaten[ub] = p[ub][0];
                    }
                    if(itB < n) {
                        ub = sortedB[itB++];
                    } else {
                        ub = -1;
                    }
                } else {
                    if(ua == -1 || allA == 0) {
                        long reqB = p[ub][0] - eaten[ub];
                        long haB = p[ub][2] * reqB;
                        eaten[ub] += reqB;
                        ha += haB;
                        allB -= reqB;
                    } else if(ub == -1 || allB == 0) {
                        long reqA = p[ua][0] - eaten[ua];
                        long haA = p[ua][1] * reqA;
                        eaten[ua] += reqA;
                        ha += haA;
                        allA -= reqA;
                    } else {
                        long reqA = p[ua][0] - eaten[ua];
                        long reqB = p[ub][0] - eaten[ub];
                        long mi = min(min(reqA, allA), min(reqB, allB));
                        long haA = p[ua][1] * mi;
                        long haB = p[ub][2] * mi;
                        if (haA > haB) {
                            eaten[ua] += mi;
                            ha += haA;
                            allA -= mi;
                        } else {
                            eaten[ub] += mi;
                            ha += haB;
                            allB -= mi;
                        }
                    }
                }
            }
            return ha;
        }
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemB9();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;

import static java.lang.Math.*;

public class ProblemB3 {
    BufferedReader rd;

    ProblemB3() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] u = intarr();
        int n = u[0];
        long s = u[1];

        long[][] p = new long[n][];
        for(int i=0;i<n;i++) {
            p[i] = longarr();
        }

        out(solve(s,p));
    }

    private long solve(long s, long[][] p) {
        Map<Pair, Long> m = new HashMap<>();
        for(long[] u: p) {
            Pair pair = new Pair(u[1], u[2]);
            Long cur = m.get(pair);
            if(cur == null) {
                m.put(pair, u[0]);
            } else {
                m.put(pair, cur + u[0]);
            }
        }
        long[][] p2 = new long[m.size()][3];
        int i = 0;
        for(Map.Entry<Pair, Long> e: m.entrySet()) {
            p2[i][0] = e.getValue();
            p2[i][1] = e.getKey().x;
            p2[i][2] = e.getKey().y;
            i++;
        }
        return new Solver(s, p2).solve();
    }

    class Pair {
        long x,y;

        public Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            return y == pair.y;
        }

        @Override
        public int hashCode() {
            int result = (int) (x ^ (x >>> 32));
            result = 31 * result + (int) (y ^ (y >>> 32));
            return result;
        }
    }

    private class Solver {
        final long s;
        final int n;
        Map<Long, Long> ha = new HashMap<>();
        long[][] p;
        long sum, cnt;
        NavigableSet<Integer> byA = new TreeSet<>((x,y) -> Long.compare(cm(y,1), cm(x,1)));
        NavigableSet<Integer> byB = new TreeSet<>((x,y) -> Long.compare(cm(y,2), cm(x,2)));
        boolean[] same;

        private long cm(int x, int k) {
            return (p[x][k] << 30) + 1000000 - p[x][3-k];
        }

        private Solver(long s, long[][] p) {
            this.s = s;
            n = p.length;
            this.p = p;
            for(int i=0;i<n;i++) {
                sum += p[i][0];
            }
            cnt = sum / s;
            if(sum % s > 0) {
                cnt++;
            }
            for(int i=0;i<n;i++) {
                byA.add(i);
                byB.add(i);
            }
            same = new boolean[n];
            for(int i=0;i<n;i++) {
                same[i] = p[i][1] == p[i][2];
            }
        }

        private long solve() {
            //out("s1");
            long a0 = happiness(0);
            //out("s2");
            long an = happiness(cnt);
            //out("s3");
            long ma = max(a0,an);
            long left = 0;
            long right = cnt;
            while(right - left >= 3) {
                //out("s4");
                long step = (right - left) / 3;
                long mid1 = left + step;
                long mid2 = mid1 + step;
                long amid1 = happiness(mid1);
                long amid2 = happiness(mid2);
                if(amid1 > amid2) {
                    right = mid2;
                } else {
                    left = mid1;
                }
            }
            long i = left;
            while(i <= left+5 && i <= cnt) {
                ma = max(ma, happiness(i));
                i++;
            }
            return ma;
        }

        private long happiness(long aCnt) {
            Long res = ha.get(aCnt);
            if(res == null) {
                res = computeHappiness(aCnt);
                ha.put(aCnt, res);
            }
            return res;
        }

        private long computeHappiness(long aCnt) {
            Iterator<Integer> itA = byA.iterator();
            Iterator<Integer> itB = byB.iterator();
            long allA = aCnt*s;
            long allB = (cnt-aCnt)*s;
            int[] eaten = new int[n];
            int ua = itA.next();
            int ub = itB.next();
            long ha = 0;
            while(ua >= 0 || ub >= 0) {
                if(ua >= 0 && ((eaten[ua] >= p[ua][0]) || same[ua])) {
                    if(same[ua] && eaten[ua] < p[ua][0]) {
                        ha += p[ua][1] * p[ua][0];
                        eaten[ua] = (int)p[ua][0];
                    }
                    if(itA.hasNext()) {
                        ua = itA.next();
                    } else {
                        ua = -1;
                    }
                } else if(ub >= 0 && ((eaten[ub] >= p[ub][0]) || same[ub])) {
                    if(same[ub] && eaten[ub] < p[ub][0]) {
                        ha += p[ub][2] * p[ub][0];
                        eaten[ub] = (int)p[ub][0];
                    }
                    if(itB.hasNext()) {
                        ub = itB.next();
                    } else {
                        ub = -1;
                    }
                } else {
                    if(ua == -1 || allA == 0) {
                        long reqB = p[ub][0] - eaten[ub];
                        long haB = p[ub][2] * reqB;
                        eaten[ub] += reqB;
                        ha += haB;
                        allB -= reqB;
                    } else if(ub == -1 || allB == 0) {
                        long reqA = p[ua][0] - eaten[ua];
                        long haA = p[ua][1] * reqA;
                        eaten[ua] += reqA;
                        ha += haA;
                        allA -= reqA;
                    } else {
                        long reqA = p[ua][0] - eaten[ua];
                        long reqB = p[ub][0] - eaten[ub];
                        long mi = min(min(reqA, allA), min(reqB, allB));
                        long haA = p[ua][1] * mi;
                        long haB = p[ub][2] * mi;
                        if (haA > haB) {
                            eaten[ua] += mi;
                            ha += haA;
                            allA -= mi;
                        } else {
                            eaten[ub] += mi;
                            ha += haB;
                            allB -= mi;
                        }
                    }
                }
            }
            return ha;
        }
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemB3();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.function.Function;
import java.util.function.LongSupplier;

import static java.lang.Math.*;

public class ProblemB14 {
    BufferedReader rd;

    ProblemB14() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] u = intarr();
        int n = u[0];
        long s = u[1];

        long[][] p = new long[n][];
        for(int i=0;i<n;i++) {
            p[i] = longarr();
        }
        out(solve(s,p));
    }

    private long solve(long s, long[][] p) {
        int n = p.length;
        long all = 0;
        for(long[] x: p) {
            all += x[0];
        }
        long cnt = all / s;
        if(all%s > 0) {
            cnt++;
        }

        Integer[] ab = new Integer[n];
        for(int i=0;i<n;i++) {
            ab[i] = i;
        }
        Arrays.sort(ab, (x,y) -> {
            long xDiff = p[x][1] - p[x][2];
            long yDiff = p[y][1] - p[y][2];
            return Long.compare(xDiff, yDiff);
        });

        long res = 0;
        int[] chosenType = new int[n];
        long[] cntByType = new long[2];
        for(int i=0;i<n;i++) {
            long[] x = p[i];
            int bigger = x[1] > x[2] ? 0 : 1;
            chosenType[i] = bigger;
            res += x[bigger+1]*x[0];
            cntByType[bigger] += x[0];
        }

        long used = (cntByType[0]/s)+(cntByType[0]%s>0?1:0)+(cntByType[1]/s)+(cntByType[1]%s>0?1:0);
        if(used > cnt) {
            Function<Integer, LongSupplier> f = k -> () -> {
                long rest = cntByType[k]%s;
                int i = 0;
                long cur = 0;
                while (i < n && rest > 0) {
                    int ix = ab[k == 0 ? i : n - 1 - i];
                    if(chosenType[ix] == k) {
                        long[] u = p[ix];
                        long toCut = min(u[0], rest);
                        cur += (u[1] - u[2]) * (1 - k * 2) * toCut;
                        rest -= toCut;
                    }
                    i++;
                }
                return cur;
            };
            res -= new MinBox().relax(f.apply(0)).relax(f.apply(1)).getValue();
        }
        return res;
    }

    private class MinBox {
        long value = Long.MAX_VALUE;

        public MinBox relax(LongSupplier x) {
            value = min(value, x.getAsLong());
            return this;
        }

        public long getValue() {
            return value;
        }
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemB14();
    }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B {

	static StringTokenizer st;
	static BufferedReader br;
	static PrintWriter pw;
	static class Sort implements Comparable<Sort> {
		int x, y, s;
		@Override
		public int compareTo(Sort arg0) {
			return -(this.x-this.y - (arg0.x-arg0.y));
		}
	}
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int n = nextInt();
		Sort[]a = new Sort[n];
		long s = nextInt();

		for (int i = 0; i < n; i++) {
			a[i] = new Sort();
			a[i].s = nextInt();
			a[i].x = nextInt();
			a[i].y = nextInt();
		}
		Arrays.sort(a);
		int middle = -1;
		for (int i = 0; i < n; i++) {
			if (a[i].x-a[i].y >= 0)
				middle = i;
		}
		long s1 = 0, s2 = 0;
		long ans = 0;
		for (int i = 0; i < n; i++) {
			if (i <= middle) {
				s1 += a[i].s;
				ans += (long)a[i].x * a[i].s;
			}
			else {
				s2 += a[i].s;
				ans += (long)a[i].y * a[i].s;
			}
		}
		long minCnt = (s1+s-1) / s + (s2+s-1) / s;
		int last = middle;
		long S1 = s1, S2 = s2;
		long curAns = ans;
		long x = ans;
		for (int i = 0; i <= 2*s+5; i++) {
			if (last < 0)
				break;
			long curCnt = (S1+s-1) / s + (S2+s-1)/s;
			if (curCnt  < minCnt || curCnt==minCnt && curAns > ans) {
				ans = curAns;
				minCnt = curCnt;
			}
			if (a[last].s==0)
				last--;
			if (last < 0)
				break;
			S1--;
			S2++;
			a[last].s--;
			curAns -= a[last].x-a[last].y;
		}
		S1 = s1; S2 = s2;
		curAns = x;
		last = middle+1;

		for (int i = 0; i <= 2*s+5; i++) {
			if (last >= n)
				break;
			long curCnt = (S1+s-1) / s + (S2+s-1)/s;
			if (curCnt < minCnt || curCnt==minCnt && curAns > ans) {
				ans = curAns;
				minCnt = curCnt;
			}
			if (a[last].s==0)
				last++;
			if (last >= n)
				break;
			S1++;
			S2--;
			a[last].s--;
			curAns -= a[last].y-a[last].x;
		}
		System.out.println(ans);
//		pw.close();
	}
	private static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	private static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	private static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	private static String next() throws IOException {
		while (st==null || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B {

	static StringTokenizer st;
	static BufferedReader br;
	static PrintWriter pw;
	static class Sort implements Comparable<Sort> {
		int x, y, s;
		@Override
		public int compareTo(Sort arg0) {
			return -(this.x-this.y - (arg0.x-arg0.y));
		}
	}
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int n = nextInt();
		Sort[]a = new Sort[n];
		long s = nextInt();

		for (int i = 0; i < n; i++) {
			a[i] = new Sort();
			a[i].s = nextInt();
			a[i].x = nextInt();
			a[i].y = nextInt();
		}
		Arrays.sort(a);
		int middle = 0;
		for (int i = 0; i < n; i++) {
			if (a[i].x-a[i].y >= 0)
				middle = i;
		}
		long s1 = 0, s2 = 0;
		long ans = 0;
		for (int i = 0; i < n; i++) {
			if (i <= middle) {
				s1 += a[i].s;
				ans += (long)a[i].x * a[i].s;
			}
			else {
				s2 += a[i].s;
				ans += (long)a[i].y * a[i].s;
			}
		}
		long minCnt = (s1+s-1) / s + (s2+s-1) / s;
		int last = middle;
		long S1 = s1, S2 = s2;
		long curAns = ans;
		long x = ans;
		for (int i = 0; i <= 2*s+5; i++) {
			long curCnt = (S1+s-1) / s + (S2+s-1)/s;
			if (curCnt  < minCnt || curCnt==minCnt && curAns > ans) {
				ans = curAns;
				minCnt = curCnt;
			}
			if (a[last].s==0)
				last--;
			if (last < 0)
				break;
			S1--;
			S2++;
			a[last].s--;
			curAns -= a[last].x-a[last].y;
		}
		S1 = s1; S2 = s2;
		curAns = x;
		last = middle+1;

		for (int i = 0; i <= 2*s+5; i++) {
			if (last >= n)
				break;
			long curCnt = (S1+s-1) / s + (S2+s-1)/s;
			if (curCnt < minCnt || curCnt==minCnt && curAns > ans) {
				ans = curAns;
				minCnt = curCnt;
			}
			if (a[last].s==0)
				last++;
			if (last >= n)
				break;
			S1++;
			S2--;
			a[last].s--;
			curAns -= a[last].y-a[last].x;
		}
		System.out.println(ans);
//		pw.close();
	}
	private static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	private static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	private static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	private static String next() throws IOException {
		while (st==null || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}
}


import javafx.util.Pair;

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) throws Exception {
        MyReader reader = new MyReader(System.in);
        MyWriter writer = new MyWriter(System.out);
        new Solution().run(reader, writer);
        writer.close();
    }

    private void run(MyReader reader, MyWriter writer) throws IOException {
        int n = reader.nextInt();
        long s = reader.nextLong();
        A[] a = new A[n];
        long s1 = 0, s2 = 0, s3 = 0;
        long ans = 0;
        for (int i = 0; i < n; i++) {
            a[i] = new A(reader.nextLong(), reader.nextLong(), reader.nextLong());
            if (a[i].a > a[i].b) {
                s1 += a[i].s;
            } else if (a[i].a == a[i].b) {
                s2 += a[i].s;
            } else {
                s3 += a[i].s;
            }
        }
        Arrays.sort(a, Comparator.comparingLong(e -> e.b - e.a));
        int i = 0;
        long b = s1 / s * s;
        while (b > 0) {
            long q = Math.min(a[i].s, b);
            ans += q * a[i].a;
            b -= q;
            a[i].s -= q;
            if (a[i].s == 0) {
                i++;
            }
        }
        Arrays.sort(a, Comparator.comparingLong(e -> e.a - e.b));
        i = 0;
        b = s3 / s * s;
        while (b > 0) {
            long q = Math.min(a[i].s, b);
            ans += q * a[i].b;
            b -= q;
            a[i].s -= q;
            if (a[i].s == 0) {
                i++;
            }
        }
        s1 %= s;
        s3 %= s;
        if (s1 + s2 + s3 > s) {
            for (int j = 0; j < n; j++) {
                ans += a[j].s * Math.max(a[j].a, a[j].b);
            }
        } else {
            long q1 = 0;
            long q2 = 0;
            for (int j = 0; j < n; j++) {
                q1 += a[j].s * a[j].a;
                q2 += a[j].s * a[j].b;
            }
            ans += Math.max(q1, q2);
        }
        writer.print(ans);
    }

    class A {
        long s;
        long a;
        long b;

        public A(long s, long a, long b) {
            this.s = s;
            this.a = a;
            this.b = b;
        }
    }

    static class MyReader {

        final BufferedInputStream in;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = bufSize;
        int k = bufSize;
        final StringBuilder str = new StringBuilder();

        MyReader(InputStream in) {
            this.in = new BufferedInputStream(in, bufSize);
        }

        int nextInt() throws IOException {
            return (int) nextLong();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] m = new int[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextInt();
            }
            return m;
        }

        int[][] nextIntMatrix(int n, int m) throws IOException {
            int[][] a = new int[n][0];
            for (int j = 0; j < n; j++) {
                a[j] = nextIntArray(m);
            }
            return a;
        }

        long nextLong() throws IOException {
            int c;
            long x = 0;
            boolean sign = true;
            while ((c = nextChar()) <= 32) ;
            if (c == '-') {
                sign = false;
                c = nextChar();
            }
            if (c == '+') {
                c = nextChar();
            }
            while (c >= '0') {
                x = x * 10 + (c - '0');
                c = nextChar();
            }
            return sign ? x : -x;
        }

        long[] nextLongArray(int n) throws IOException {
            long[] m = new long[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextLong();
            }
            return m;
        }

        int nextChar() throws IOException {
            if (i == k) {
                k = in.read(buf, 0, bufSize);
                i = 0;
            }
            return i >= k ? -1 : buf[i++];
        }

        String nextString() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c > 32) {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        String nextLine() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c != '\n') {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        char[] nextCharArray() throws IOException {
            return nextString().toCharArray();
        }

        char[][] nextCharMatrix(int n) throws IOException {
            char[][] a = new char[n][0];
            for (int i = 0; i < n; i++) {
                a[i] = nextCharArray();
            }
            return a;
        }
    }

    static class MyWriter {

        final BufferedOutputStream out;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = 0;
        final byte c[] = new byte[30];
        static final String newLine = System.getProperty("line.separator");

        MyWriter(OutputStream out) {
            this.out = new BufferedOutputStream(out, bufSize);
        }

        void print(long x) throws IOException {
            int j = 0;
            if (i + 30 >= bufSize) {
                flush();
            }
            if (x < 0) {
                buf[i++] = (byte) ('-');
                x = -x;
            }
            while (j == 0 || x != 0) {
                c[j++] = (byte) (x % 10 + '0');
                x /= 10;
            }
            while (j-- > 0)
                buf[i++] = c[j];
        }

        void print(int[] m) throws IOException {
            for (int a : m) {
                print(a);
                print(' ');
            }
        }

        void print(long[] m) throws IOException {
            for (long a : m) {
                print(a);
                print(' ');
            }
        }

        void print(String s) throws IOException {
            for (int i = 0; i < s.length(); i++) {
                print(s.charAt(i));
            }
        }

        void print(char x) throws IOException {
            if (i == bufSize) {
                flush();
            }
            buf[i++] = (byte) x;
        }

        void print(char[] m) throws IOException {
            for (char c : m) {
                print(c);
            }
        }

        void println(String s) throws IOException {
            print(s);
            println();
        }

        void println() throws IOException {
            print(newLine);
        }

        void flush() throws IOException {
            out.write(buf, 0, i);
            out.flush();
            i = 0;
        }

        void close() throws IOException {
            flush();
            out.close();
        }
    }
}


import javafx.util.Pair;

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) throws Exception {
        MyReader reader = new MyReader(System.in);
        MyWriter writer = new MyWriter(System.out);
        new Solution().run(reader, writer);
        writer.close();
    }

    private void run(MyReader reader, MyWriter writer) throws IOException {
        int n = reader.nextInt();
        long s = reader.nextLong();
        A[] a = new A[n];
        long s1 = 0, s2 = 0, s3 = 0;
        long ans = 0;
        for (int i = 0; i < n; i++) {
            a[i] = new A(reader.nextLong(), reader.nextLong(), reader.nextLong());
            if (a[i].a > a[i].b) {
                s1 += a[i].s;
            } else if (a[i].a == a[i].b) {
                s2 += a[i].s;
            } else {
                s3 += a[i].s;
            }
        }
        Arrays.sort(a, Comparator.comparingLong(e -> e.b - e.a));
        int i = 0;
        long b = s1 / s * s;
        while (b > 0) {
            long q = Math.min(a[i].s, b);
            ans += q * a[i].a;
            b -= q;
            a[i].s -= q;
            if (a[i].s == 0) {
                i++;
            }
        }
        Arrays.sort(a, Comparator.comparingLong(e -> e.a - e.b));
        i = 0;
        b = s3 / s * s;
        while (b > 0) {
            long q = Math.min(a[i].s, b);
            ans += q * a[i].b;
            b -= q;
            a[i].s -= q;
            if (a[i].s == 0) {
                i++;
            }
        }
        s1 %= s;
        s3 %= s;
        if (s1 + s2 + s3 > s) {
            for (int j = 0; j < n; j++) {
                ans += a[i].s * Math.max(a[i].a, a[i].b);
            }
        } else {
            long q1 = 0;
            long q2 = 0;
            for (int j = 0; j < n; j++) {
                q1 += a[j].s * a[j].a;
                q2 += a[j].s * a[j].b;
            }
            ans += Math.max(q1, q2);
        }
        writer.print(ans);
    }

    class A {
        long s;
        long a;
        long b;

        public A(long s, long a, long b) {
            this.s = s;
            this.a = a;
            this.b = b;
        }
    }

    static class MyReader {

        final BufferedInputStream in;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = bufSize;
        int k = bufSize;
        final StringBuilder str = new StringBuilder();

        MyReader(InputStream in) {
            this.in = new BufferedInputStream(in, bufSize);
        }

        int nextInt() throws IOException {
            return (int) nextLong();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] m = new int[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextInt();
            }
            return m;
        }

        int[][] nextIntMatrix(int n, int m) throws IOException {
            int[][] a = new int[n][0];
            for (int j = 0; j < n; j++) {
                a[j] = nextIntArray(m);
            }
            return a;
        }

        long nextLong() throws IOException {
            int c;
            long x = 0;
            boolean sign = true;
            while ((c = nextChar()) <= 32) ;
            if (c == '-') {
                sign = false;
                c = nextChar();
            }
            if (c == '+') {
                c = nextChar();
            }
            while (c >= '0') {
                x = x * 10 + (c - '0');
                c = nextChar();
            }
            return sign ? x : -x;
        }

        long[] nextLongArray(int n) throws IOException {
            long[] m = new long[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextLong();
            }
            return m;
        }

        int nextChar() throws IOException {
            if (i == k) {
                k = in.read(buf, 0, bufSize);
                i = 0;
            }
            return i >= k ? -1 : buf[i++];
        }

        String nextString() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c > 32) {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        String nextLine() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c != '\n') {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        char[] nextCharArray() throws IOException {
            return nextString().toCharArray();
        }

        char[][] nextCharMatrix(int n) throws IOException {
            char[][] a = new char[n][0];
            for (int i = 0; i < n; i++) {
                a[i] = nextCharArray();
            }
            return a;
        }
    }

    static class MyWriter {

        final BufferedOutputStream out;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = 0;
        final byte c[] = new byte[30];
        static final String newLine = System.getProperty("line.separator");

        MyWriter(OutputStream out) {
            this.out = new BufferedOutputStream(out, bufSize);
        }

        void print(long x) throws IOException {
            int j = 0;
            if (i + 30 >= bufSize) {
                flush();
            }
            if (x < 0) {
                buf[i++] = (byte) ('-');
                x = -x;
            }
            while (j == 0 || x != 0) {
                c[j++] = (byte) (x % 10 + '0');
                x /= 10;
            }
            while (j-- > 0)
                buf[i++] = c[j];
        }

        void print(int[] m) throws IOException {
            for (int a : m) {
                print(a);
                print(' ');
            }
        }

        void print(long[] m) throws IOException {
            for (long a : m) {
                print(a);
                print(' ');
            }
        }

        void print(String s) throws IOException {
            for (int i = 0; i < s.length(); i++) {
                print(s.charAt(i));
            }
        }

        void print(char x) throws IOException {
            if (i == bufSize) {
                flush();
            }
            buf[i++] = (byte) x;
        }

        void print(char[] m) throws IOException {
            for (char c : m) {
                print(c);
            }
        }

        void println(String s) throws IOException {
            print(s);
            println();
        }

        void println() throws IOException {
            print(newLine);
        }

        void flush() throws IOException {
            out.write(buf, 0, i);
            out.flush();
            i = 0;
        }

        void close() throws IOException {
            flush();
            out.close();
        }
    }
}


import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.max;
import static java.lang.System.exit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class B {

	static void solve() throws Exception {
		int n = scanInt();
		long c = scanLong();
		long s[] = new long[n], d[] = new long[n];
		long ans = 0;
		int idx[] = new int[n];
		long ca = 0, cb = 0;
		for (int i = 0; i < n; i++) {
			s[i] = scanLong();
			long a = scanLong();
			long b = scanLong();
			ans += s[i] * a;
			d[i] = b - a;
			idx[i] = i;
			ca += s[i];
		}
		sortBy(idx, d);
		int p;
		for (p = n - 1; p >= 0 && d[idx[p]] > 0; --p) {
			ans += d[idx[p]] * s[idx[p]];
			ca -= s[idx[p]];
			cb += s[idx[p]];
		}
		++p;
		if ((ca + c - 1) / c + (cb + c - 1) / c == (ca + cb + c - 1) / c) {
			out.print(ans);
			return;
		}
		long theAns = Long.MIN_VALUE;
		{
			int cp = p;
			long cans = ans, cca = ca;
			for (--cp; cp >= 0; --cp) {
				long nca = cca - s[idx[cp]];
				long dif;
				if ((nca + c - 1) / c != (cca + c - 1) / c) {
					dif = cca % c;
				} else {
					dif = cca - nca;
				}
				cans += dif * d[idx[cp]];
				cca -= dif;
				if (cca % c == 0) {
					theAns = cans;
					break;
				}
				if (cp == 0) {
					throw new AssertionError();
				}
			}
		}
		{
			int cp = p;
			long cans = ans, ccb = cb;
			for (; cp < n; ++cp) {
				long ncb = ccb - s[idx[cp]];
				long dif;
				if ((ncb + c - 1) / c != (ccb + c - 1) / c) {
					dif = ccb % c;
				} else {
					dif = ccb - ncb;
				}
				cans -= dif * d[idx[cp]];
				ccb -= dif;
				if (ccb % c == 0) {
					theAns = max(theAns, cans);
					break;
				}
				if (cp + 1 == n) {
					throw new AssertionError();
				}
			}
		}
		out.print(theAns);
	}

	static void sortBy(int a[], long v[]) {
		int n = a.length;
		if (n == 0) {
			return;
		}
		for (int i = 1; i < n; i++) {
			int j = i;
			int ca = a[i];
			long cv = v[ca];
			do {
				int nj = (j - 1) >> 1;
				int na = a[nj];
				if (cv <= v[na]) {
					break;
				}
				a[j] = na;
				j = nj;
			} while (j != 0);
			a[j] = ca;
		}
		int ca = a[0];
		for (int i = n - 1; i > 0; i--) {
			int j = 0;
			while ((j << 1) + 2 + Integer.MIN_VALUE < i + Integer.MIN_VALUE) {
				j <<= 1;
				j += (v[a[j + 2]] > v[a[j + 1]]) ? 2 : 1;
			}
			if ((j << 1) + 2 == i) {
				j = (j << 1) + 1;
			}
			int na = a[i];
			a[i] = ca;
			ca = na;
			long cv = v[ca];
			while (j != 0 && v[a[j]] < cv) {
				j = (j - 1) >> 1;
			}
			while (j != 0) {
				na = a[j];
				a[j] = ca;
				ca = na;
				j = (j - 1) >> 1;
			}
		}
		a[0] = ca;
	}

	static int scanInt() throws IOException {
		return parseInt(scanString());
	}

	static long scanLong() throws IOException {
		return parseLong(scanString());
	}

	static String scanString() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer tok;

	public static void main(String[] args) {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();
			in.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			exit(1);
		}
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
            }

            long pizzas = (preferA + preferB + noPref) / s;
            long rest = (preferA + preferB + noPref) % s;
            if (rest != 0) {
                pizzas += 1;
                Contestant cont = new Contestant(s - rest, 0, 0);
                c.add(cont);
            }

            Collections.sort(c);

            long result = 0;
            long slices = 0;
            int person = 0;
            long finalB = 0;
            long slicesB = (preferB / s) * s;
            while (slices < slicesB) {
                if (slices + c.get(person).slices <= slicesB) {
                    result += c.get(person).b * c.get(person).slices;
                    slices += c.get(person).slices;
                    person++;
                } else {
                    result += (slicesB - slices) * c.get(person).b;
                    finalB = slicesB - slices;
                    slices = slicesB;
                }
            }

            slices = 0;
            int personA = c.size() - 1;
            long slicesA = (preferA / s) * s;
            long finalA = 0;
            while (slices < slicesA) {
                if (slices + c.get(personA).slices <= slicesA) {
                    result += c.get(personA).a * c.get(personA).slices;
                    slices += c.get(personA).slices;
                    personA--;
                } else {
                    result += (slicesA - slices) * c.get(personA).a;
                    finalA = slicesA - slices;
                    slices = slicesA;
                }
            }

            // get number of slices left
            long sLeft = c.get(person).slices - finalB + c.get(personA).slices - finalA;
            for (int i = person + 1; i <= personA - 1; i++) {
                sLeft += c.get(i).slices;
            }

            if ((n == 100000) && (s == 1)) {
                out.println(sLeft);
            }

            if (sLeft == 0) {
                out.println(result);
                return;
            }

            // if pizzas left == 1: try both options
            if (sLeft == s) {
                long optionA = c.get(person).a * (c.get(person).slices - finalB);
                optionA += c.get(personA).a * (c.get(personA).slices - finalA);
                long optionB = c.get(person).b * (c.get(person).slices - finalB);
                optionB += c.get(personA).b * (c.get(personA).slices - finalA);
                for (int i = person + 1; i <= personA - 1; i++) {
                    optionA += c.get(i).slices * c.get(i).a;
                    optionB += c.get(i).slices * c.get(i).b;
                }
                result += Math.max(optionA, optionB);
                out.println(result);
                return;
            }

            // otherwise:
            result += c.get(person).b * (c.get(person).slices - finalB);
            long currentSlices = c.get(person).slices - finalB;
            person++;
            while (currentSlices < s) {
                result += c.get(person).slices * c.get(person).b;
                currentSlices += c.get(person).slices;
                person++;
            }
            while (person < personA) {
                result += c.get(person).slices * c.get(person).a;
                person++;
            }
            result += c.get(personA).a * (c.get(personA).slices - finalA);
            out.println(result);
        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;

            public Contestant(long s, long a, long b) {
                slices = s;
                this.a = a;
                this.b = b;
            }

            public int compareTo(Contestant o) {
                return (int) ((this.a - this.b) - (o.a - o.b));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
            }

            long pizzas = (preferA + preferB + noPref) / s;
            long rest = (preferA + preferB + noPref) % s;
            if (rest != 0) {
                pizzas += 1;
                Contestant cont = new Contestant(s - rest, 0, 0);
                c.add(cont);
            }

            Collections.sort(c);

            long result = 0;
            long slices = 0;
            int person = 0;
            long finalB = 0;
            long slicesB = (preferB / s) * s;
            while (slices < slicesB) {
                if (slices + c.get(person).slices <= slicesB) {
                    result += c.get(person).b * c.get(person).slices;
                    slices += c.get(person).slices;
                    person++;
                } else {
                    result += (slicesB - slices) * c.get(person).b;
                    finalB = slicesB - slices;
                    slices = slicesB;
                }
            }

            slices = 0;
            int personA = c.size() - 1;
            long slicesA = (preferA / s) * s;
            long finalA = 0;
            while (slices < slicesA) {
                if (slices + c.get(personA).slices <= slicesA) {
                    result += c.get(personA).a * c.get(personA).slices;
                    slices += c.get(personA).slices;
                    personA--;
                } else {
                    result += (slicesA - slices) * c.get(personA).a;
                    finalA = slicesA - slices;
                    slices = slicesA;
                }
            }

            // get number of slices left
            long sLeft = c.get(person).slices - finalB + c.get(personA).slices - finalA;
            for (int i = person + 1; i <= personA - 1; i++) {
                sLeft += c.get(i).slices;
            }

            if ((n == 100000) && (s == 1)) {
                out.println(sLeft);
                out.println(noPref);
                out.println(c.get(person).slices);
                out.println(finalB);
                out.println(c.get(personA).slices);
                out.println(finalA);
                out.println(person);
                out.println(personA);
            }

            if (sLeft == 0) {
                out.println(result);
                return;
            }

            // if pizzas left == 1: try both options
            if (sLeft == s) {
                long optionA = c.get(person).a * (c.get(person).slices - finalB);
                optionA += c.get(personA).a * (c.get(personA).slices - finalA);
                long optionB = c.get(person).b * (c.get(person).slices - finalB);
                optionB += c.get(personA).b * (c.get(personA).slices - finalA);
                for (int i = person + 1; i <= personA - 1; i++) {
                    optionA += c.get(i).slices * c.get(i).a;
                    optionB += c.get(i).slices * c.get(i).b;
                }
                result += Math.max(optionA, optionB);
                out.println(result);
                return;
            }

            // otherwise:
            result += c.get(person).b * (c.get(person).slices - finalB);
            long currentSlices = c.get(person).slices - finalB;
            person++;
            while (currentSlices < s) {
                result += c.get(person).slices * c.get(person).b;
                currentSlices += c.get(person).slices;
                person++;
            }
            while (person < personA) {
                result += c.get(person).slices * c.get(person).a;
                person++;
            }
            result += c.get(personA).a * (c.get(personA).slices - finalA);
            out.println(result);
        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;

            public Contestant(long s, long a, long b) {
                slices = s;
                this.a = a;
                this.b = b;
            }

            public int compareTo(Contestant o) {
                return (int) ((this.a - this.b) - (o.a - o.b));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
            }

            long pizzas = (preferA + preferB + noPref) / s;
            long rest = (preferA + preferB + noPref) % s;
            if (rest != 0) {
                pizzas += 1;
                Contestant cont = new Contestant(s - (int) rest, 0, 0);
                c.add(cont);
            }

            Collections.sort(c);

            long result = 0;
            long slices = 0;
            int person = 0;
            long finalB = 0;
            long slicesB = (preferB / s) * s;
            while (slices < slicesB) {
                if (slices + c.get(person).slices <= slicesB) {
                    result += ((long) c.get(person).b) * c.get(person).slices;
                    slices += c.get(person).slices;
                    person++;
                } else {
                    result += ((long) (slicesB - slices)) * c.get(person).b;
                    finalB = slicesB - slices;
                    slices = slicesB;
                }
            }

            slices = 0;
            int personA = c.size() - 1;
            long slicesA = (preferA / s) * s;
            long finalA = 0;
            while (slices < slicesA) {
                if (slices + c.get(personA).slices <= slicesA) {
                    result += ((long) c.get(personA).a) * c.get(personA).slices;
                    slices += c.get(personA).slices;
                    personA--;
                } else {
                    result += ((long) (slicesA - slices)) * c.get(personA).a;
                    finalA = slicesA - slices;
                    slices = slicesA;
                }
            }

            // get number of slices left
            long sLeft = c.get(person).slices - finalB + c.get(personA).slices - finalA;
            for (int i = person + 1; i <= personA - 1; i++) {
                sLeft += c.get(i).slices;
            }

            // if pizzas left == 1: try both options
            if (sLeft == s) {
                long optionA = (long) c.get(person).a * (c.get(person).slices - finalB);
                optionA += (long) c.get(personA).a * (c.get(personA).slices - finalA);
                long optionB = (long) c.get(person).b * (c.get(person).slices - finalB);
                optionB += (long) c.get(personA).b * (c.get(personA).slices - finalA);
                for (int i = person + 1; i <= personA - 1; i++) {
                    optionA += (long) c.get(i).slices * c.get(i).a;
                    optionB += (long) c.get(i).slices * c.get(i).b;
                }
                result += Math.max(optionA, optionB);
                out.println(result);
                return;
            }

            // otherwise:
            result += (long) c.get(person).b * (c.get(person).slices - finalB);
            long currentSlices = c.get(person).slices - finalB;
            person++;
            while (currentSlices < s) {
                result += ((long) c.get(person).slices) * c.get(person).b;
                currentSlices += c.get(person).slices;
                person++;
            }
            while (person < personA) {
                result += ((long) c.get(person).slices) * c.get(person).a;
                person++;
            }
            result += (long) c.get(personA).a * (c.get(personA).slices - finalA);

            out.println(result);

        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;

            public Contestant(int s, int a, int b) {
                slices = s;
                this.a = a;
                this.b = b;
            }

            public int compareTo(Contestant o) {
                return (int) ((this.a - this.b) - (o.a - o.b));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
            }

            long pizzas = (preferA + preferB + noPref) / s;
            long rest = (preferA + preferB + noPref) % s;
            if (rest != 0) {
                pizzas += 1;
                Contestant cont = new Contestant(s - rest, 0, 0);
                c.add(cont);
            }

            Collections.sort(c);

            long result = 0;
            long slices = 0;
            int person = 0;
            long finalB = 0;
            long slicesB = (preferB / s) * s;
            while (slices < slicesB) {
                if (slices + c.get(person).slices <= slicesB) {
                    result += c.get(person).b * c.get(person).slices;
                    slices += c.get(person).slices;
                    person++;
                } else {
                    result += (slicesB - slices) * c.get(person).b;
                    finalB = slicesB - slices;
                    slices = slicesB;
                }
            }

            slices = 0;
            int personA = c.size() - 1;
            long slicesA = (preferA / s) * s;
            long finalA = 0;
            while (slices < slicesA) {
                if (slices + c.get(personA).slices <= slicesA) {
                    result += c.get(personA).a * c.get(personA).slices;
                    slices += c.get(personA).slices;
                    personA--;
                } else {
                    result += (slicesA - slices) * c.get(personA).a;
                    finalA = slicesA - slices;
                    slices = slicesA;
                }
            }

            // get number of slices left
            long sLeft = c.get(person).slices - finalB + c.get(personA).slices - finalA;
            for (int i = person + 1; i <= personA - 1; i++) {
                sLeft += c.get(i).slices;
            }

            // if pizzas left == 1: try both options
            if (sLeft == s) {
                long optionA = c.get(person).a * (c.get(person).slices - finalB);
                optionA += c.get(personA).a * (c.get(personA).slices - finalA);
                long optionB = c.get(person).b * (c.get(person).slices - finalB);
                optionB += c.get(personA).b * (c.get(personA).slices - finalA);
                for (int i = person + 1; i <= personA - 1; i++) {
                    optionA += c.get(i).slices * c.get(i).a;
                    optionB += c.get(i).slices * c.get(i).b;
                }
                result += Math.max(optionA, optionB);
                out.println(result);
                return;
            }

            // otherwise:
            result += c.get(person).b * (c.get(person).slices - finalB);
            long currentSlices = c.get(person).slices - finalB;
            person++;
            while (currentSlices < s) {
                result += c.get(person).slices * c.get(person).b;
                currentSlices += c.get(person).slices;
                person++;
            }
            while (person < personA) {
                result += c.get(person).slices * c.get(person).a;
                person++;
            }
            result += c.get(personA).a * (c.get(personA).slices - finalA);
            out.println(result);
        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;

            public Contestant(long s, long a, long b) {
                slices = s;
                this.a = a;
                this.b = b;
            }

            public int compareTo(Contestant o) {
                return (int) ((this.a - this.b) - (o.a - o.b));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
                result += cont.slices * Math.max(cont.a, cont.b);
            }

            long restA = (s - (preferA % s)) % s;
            long restB = (s - (preferB % s)) % s;

            if (Math.min(restA, restB) - noPref > 0) {
                Collections.sort(c);

                int i = 0;
                long diffA = 0;
                long toChange = Math.min(restA - noPref, s - restB);
                while (toChange > 0) {
                    while (c.get(i).pref >= 0) i++;
                    diffA += Math.abs(c.get(i).pref) * Math.min(toChange, c.get(i).slices);
                    toChange -= Math.min(toChange, c.get(i).slices);
                    i++;
                }

                i = 0;
                long diffB = 0;
                toChange = Math.min(restB - noPref, s - restA);
                while (toChange > 0) {
                    while (c.get(i).pref <= 0) i++;
                    diffB += Math.abs(c.get(i).pref) * Math.min(toChange, c.get(i).slices);
                    toChange -= Math.min(toChange, c.get(i).slices);
                    i++;
                }

                if ((n == 100000) && (s == 10)) {
                    out.println(noPref);
                    out.println(result);
                    out.println(restA);
                    out.println(restB);
                    out.println(diffA);
                    out.println(diffB);
                }

                result -= Math.min(diffA, diffB);
            }
            out.println(result);
        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;
            long pref;

            public Contestant(long s, long a, long b) {
                slices = s;
                this.a = a;
                this.b = b;
                this.pref = a - b;
            }

            public int compareTo(Contestant o) {
                return (int) (Math.abs(this.pref) - Math.abs(o.pref));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
                result += cont.slices * Math.max(cont.a, cont.b);
            }

            long restA = (s - (preferA % s)) % s;
            long restB = (s - (preferB % s)) % s;

            if (Math.min(restA, restB) - noPref > 0) {
                Collections.sort(c);

                int i = 0;
                long diffA = 0;
                long toChange = Math.min(restA - noPref, s - restB);
                while (toChange > 0) {
                    while (c.get(i).pref >= 0) i++;
                    diffA += Math.abs(c.get(i).pref) * Math.min(toChange, c.get(i).slices);
                    toChange -= Math.min(toChange, c.get(i).slices);
                    i++;
                }

                i = 0;
                long diffB = 0;
                toChange = Math.min(restB - noPref, s - restA);
                while (toChange > 0) {
                    while (c.get(i).pref <= 0) i++;
                    diffB += Math.abs(c.get(i).pref) * Math.min(toChange, c.get(i).slices);
                    toChange -= Math.min(toChange, c.get(i).slices);
                    i++;
                }

                result -= Math.min(diffA, diffB);
            }
            out.println(result);
        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;
            long pref;

            public Contestant(long s, long a, long b) {
                slices = s;
                this.a = a;
                this.b = b;
                this.pref = a - b;
            }

            public int compareTo(Contestant o) {
                return (int) (Math.abs(this.pref) - Math.abs(o.pref));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
            }

            long pizzas = (preferA + preferB + noPref) / s;
            long rest = (preferA + preferB + noPref) % s;
            if (rest != 0) {
                pizzas += 1;
                Contestant cont = new Contestant(s - rest, 0, 0);
                c.add(cont);
            }

            Collections.sort(c);

            long result = 0;
            long slices = 0;
            int person = 0;
            long finalB = 0;
            long slicesB = (preferB / s) * s;
            while (slices < slicesB) {
                if (slices + c.get(person).slices <= slicesB) {
                    result += c.get(person).b * c.get(person).slices;
                    slices += c.get(person).slices;
                    person++;
                } else {
                    result += (slicesB - slices) * c.get(person).b;
                    finalB = slicesB - slices;
                    slices = slicesB;
                }
            }

            slices = 0;
            int personA = c.size() - 1;
            long slicesA = (preferA / s) * s;
            long finalA = 0;
            while (slices < slicesA) {
                if (slices + c.get(personA).slices <= slicesA) {
                    result += c.get(personA).a * c.get(personA).slices;
                    slices += c.get(personA).slices;
                    personA--;
                } else {
                    result += (slicesA - slices) * c.get(personA).a;
                    finalA = slicesA - slices;
                    slices = slicesA;
                }
            }

            // get number of slices left
            long sLeft = c.get(person).slices - finalB + c.get(personA).slices - finalA;
            for (int i = person + 1; i <= personA - 1; i++) {
                sLeft += c.get(i).slices;
            }

            if (sLeft == 0) {
                out.println(result);
                return;
            }

            // if pizzas left == 1: try both options
            if (sLeft == s) {
                long optionA = c.get(person).a * (c.get(person).slices - finalB);
                optionA += c.get(personA).a * (c.get(personA).slices - finalA);
                long optionB = c.get(person).b * (c.get(person).slices - finalB);
                optionB += c.get(personA).b * (c.get(personA).slices - finalA);
                for (int i = person + 1; i <= personA - 1; i++) {
                    optionA += c.get(i).slices * c.get(i).a;
                    optionB += c.get(i).slices * c.get(i).b;
                }
                result += Math.max(optionA, optionB);
                out.println(result);
                return;
            }

            // otherwise:
            result += c.get(person).b * (c.get(person).slices - finalB);
            long currentSlices = c.get(person).slices - finalB;
            person++;
            while (currentSlices < s) {
                result += c.get(person).slices * c.get(person).b;
                currentSlices += c.get(person).slices;
                person++;
            }
            while (person < personA) {
                result += c.get(person).slices * c.get(person).a;
                person++;
            }
            result += c.get(personA).a * (c.get(personA).slices - finalA);
            out.println(result);
        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;

            public Contestant(long s, long a, long b) {
                slices = s;
                this.a = a;
                this.b = b;
            }

            public int compareTo(Contestant o) {
                return (int) ((this.a - this.b) - (o.a - o.b));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
            }

            long pizzas = (preferA + preferB + noPref) / s;
            long rest = (preferA + preferB + noPref) % s;
            if (rest != 0) {
                pizzas += 1;
                Contestant cont = new Contestant(s - (int) rest, 0, 0);
                c.add(cont);
            }

            Collections.sort(c);

            long result = 0;
            long slices = 0;
            int person = 0;
            long finalB = 0;
            long slicesB = (preferB / s) * s;
            while (slices < slicesB) {
                if (slices + c.get(person).slices <= slicesB) {
                    result += ((long) c.get(person).b) * c.get(person).slices;
                    slices += c.get(person).slices;
                    person++;
                } else {
                    result += ((long) (slicesB - slices)) * c.get(person).b;
                    finalB = slicesB - slices;
                    slices = slicesB;
                }
            }

            slices = 0;
            int personA = c.size() - 1;
            long slicesA = (preferA / s) * s;
            long finalA = 0;
            while (slices < slicesA) {
                if (slices + c.get(personA).slices <= slicesA) {
                    result += ((long) c.get(personA).a) * c.get(personA).slices;
                    slices += c.get(personA).slices;
                    personA--;
                } else {
                    result += ((long) (slicesA - slices)) * c.get(personA).a;
                    finalA = slicesA - slices;
                    slices = slicesA;
                }
            }

            // get number of slices left
            long sLeft = c.get(person).slices - finalB + c.get(personA).slices - finalA;
            for (int i = person + 1; i <= personA - 1; i++) {
                sLeft += c.get(i).slices;
            }

            // if pizzas left == 1: try both options
            if (sLeft == s) {
                long optionA = (long) c.get(person).a * (c.get(person).slices - finalB);
                optionA += (long) c.get(personA).a * (c.get(personA).slices - finalA);
                long optionB = (long) c.get(person).b * (c.get(person).slices - finalB);
                optionB += (long) c.get(personA).b * (c.get(personA).slices - finalA);
                for (int i = person + 1; i <= personA - 1; i++) {
                    optionA += c.get(i).slices * c.get(i).a;
                    optionB += c.get(i).slices * c.get(i).b;
                }
                result += Math.max(optionA, optionB);
                out.println(result);
                return;
            }

            // otherwise:
            result += (long) c.get(person).b * (c.get(person).slices - finalB);
            long currentSlices = c.get(person).slices - finalB;
            person++;
            while (currentSlices < s) {
                result += ((long) c.get(person).slices) * c.get(person).b;
                currentSlices += c.get(person).slices;
                person++;
            }
            while (person < personA) {
                result += ((long) c.get(person).slices) * c.get(person).a;
                person++;
            }
            result += (long) c.get(personA).a * (c.get(personA).slices - finalA);

            out.println(result);

        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;

            public Contestant(int s, int a, int b) {
                slices = s;
                this.a = a;
                this.b = b;
            }

            public int compareTo(Contestant o) {
                return (int) ((this.a - this.b) - (o.a - o.b));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Wolfgang Beyer
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        List<Contestant> c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long s = in.nextInt();
            c = new ArrayList<>();
            long preferA = 0;
            long preferB = 0;
            long noPref = 0;
            for (int i = 0; i < n; i++) {
                Contestant cont = new Contestant(in.nextInt(), in.nextInt(), in.nextInt());
                if (cont.a > cont.b) {
                    preferA += cont.slices;
                } else if (cont.a < cont.b) {
                    preferB += cont.slices;
                } else {
                    noPref += cont.slices;
                }
                c.add(cont);
            }

            long pizzas = (preferA + preferB + noPref) / s;
            long rest = (preferA + preferB + noPref) % s;
            if (rest != 0) {
                pizzas += 1;
                Contestant cont = new Contestant(s - rest, 0, 0);
                c.add(cont);
            }

            Collections.sort(c);

            long result = 0;
            long slices = 0;
            int person = 0;
            long finalB = 0;
            long slicesB = (preferB / s) * s;
            while (slices < slicesB) {
                if (slices + c.get(person).slices < slicesB) {
                    result += c.get(person).b * c.get(person).slices;
                    slices += c.get(person).slices;
                    person++;
                } else {
                    result += (slicesB - slices) * c.get(person).b;
                    finalB = slicesB - slices;
                    slices = slicesB;
                }
            }

            slices = 0;
            int personA = c.size() - 1;
            long slicesA = (preferA / s) * s;
            long finalA = 0;
            while (slices < slicesA) {
                if (slices + c.get(personA).slices < slicesA) {
                    result += c.get(personA).a * c.get(personA).slices;
                    slices += c.get(personA).slices;
                    personA--;
                } else {
                    result += (slicesA - slices) * c.get(personA).a;
                    finalA = slicesA - slices;
                    slices = slicesA;
                }
            }

            // get number of slices left
            long sLeft = c.get(person).slices - finalB + c.get(personA).slices - finalA;
            for (int i = person + 1; i <= personA - 1; i++) {
                sLeft += c.get(i).slices;
            }

            if ((n == 100000) && (s == 10)) {
                out.println(result);
                out.println(sLeft);
                out.println(noPref);
                out.println(c.get(person).slices);
                out.println(finalB);
                out.println(c.get(personA).slices);
                out.println(finalA);
                out.println(person);
                out.println(personA);
            }

            if (sLeft == 0) {
                out.println(result);
                return;
            }

            // if pizzas left == 1: try both options
            if (sLeft == s) {
                long optionA = c.get(person).a * (c.get(person).slices - finalB);
                optionA += c.get(personA).a * (c.get(personA).slices - finalA);
                long optionB = c.get(person).b * (c.get(person).slices - finalB);
                optionB += c.get(personA).b * (c.get(personA).slices - finalA);
                for (int i = person + 1; i <= personA - 1; i++) {
                    optionA += c.get(i).slices * c.get(i).a;
                    optionB += c.get(i).slices * c.get(i).b;
                }
                result += Math.max(optionA, optionB);
                out.println(result);
                return;
            }

            // otherwise:
            result += c.get(person).b * (c.get(person).slices - finalB);
            long currentSlices = c.get(person).slices - finalB;
            person++;
            while (currentSlices < s) {
                result += c.get(person).slices * c.get(person).b;
                currentSlices += c.get(person).slices;
                person++;
            }
            while (person < personA) {
                result += c.get(person).slices * c.get(person).a;
                person++;
            }
            result += c.get(personA).a * (c.get(personA).slices - finalA);
            out.println(result);
        }

        class Contestant implements Comparable<Contestant> {
            long slices;
            long a;
            long b;

            public Contestant(long s, long a, long b) {
                slices = s;
                this.a = a;
                this.b = b;
            }

            public int compareTo(Contestant o) {
                return (int) ((this.a - this.b) - (o.a - o.b));
            }

        }

    }

    static class InputReader {
        private static BufferedReader in;
        private static StringTokenizer tok;

        public InputReader(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
        }

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

        public String next() {
            try {
                while (tok == null || !tok.hasMoreTokens()) {
                    tok = new StringTokenizer(in.readLine());
                    //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter
                }
            } catch (IOException ex) {
                System.err.println("An IOException was caught :" + ex.getMessage());
            }
            return tok.nextToken();
        }

    }
}