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;
                }
            }

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

            // 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;
            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) {
            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(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.*;
import java.util.*;

public class B {

    static class Player implements Comparable<Player> {
        int cost;
        int cnt;

        @Override
        public int compareTo(Player o) {
            return Integer.compare(cost, o.cost);
        }

        public Player(int cost, int cnt) {
            this.cost = cost;
            this.cnt = cnt;
        }
    }

    void submit() {
        int n = nextInt();
        int pizza = nextInt();

        List<Player>[] lst = new List[2];
        lst[0] = new ArrayList<>();
        lst[1] = new ArrayList<>();

        long base = 0;

        long c1 = 0;
        long c2 = 0;

        for (int i = 0; i < n; i++) {
            int x = nextInt();
            int costA = nextInt();
            int costB = nextInt();
            base += (long)x * Math.max(costA, costB);
            if (costA >= costB) {
                lst[0].add(new Player(costA - costB, x));
                c1 += x;
            } else {
                lst[1].add(new Player(costB - costA, x));
                c2 += x;
            }
        }

        for (List<Player> l : lst) {
            Collections.sort(l);
        }

        if ((n == 100000) && (pizza == 10)) {
            out.println(base);
        }

        long ret = Math.min(go(lst[0], c1, c2, pizza), go(lst[1], c2, c1, pizza));
        out.println(base - ret);
    }

    long go(List<Player> lst, long c1, long c2, int pizza) {
        long opt = ceil(c1 + c2, pizza);
        if (ceil(c1, pizza) + ceil(c2, pizza) == opt) {
            return 0;
        }
        long ret = 0;
        for (Player p : lst) {
            for (int i = 0; i < p.cnt; i++) {
                ret += p.cost;
                c1 -= 1;
                c2 += 1;
                if (ceil(c1, pizza) + ceil(c2, pizza) == opt) {
                    return ret;
                }
            }
        }
        throw new AssertionError();
    }

    long ceil(long a, long b) {
        return (a + b - 1) / b;
    }

    void preCalc() {

    }

    void stress() {

    }

    void test() {

    }

    B() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        preCalc();
        submit();
        //stress();
        //test();
        out.close();
    }

    static final Random rng = new Random();

    static int rand(int l, int r) {
        return l + rng.nextInt(r - l + 1);
    }

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

    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;

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

    String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

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

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

    double nextDouble() {
        return Double.parseDouble(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(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) {
            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 ((restA + restB >= s) && (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.*;
import java.util.*;

public class Template implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine(), " :");
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    int[] readIntArray(int size) throws IOException {
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    <T> List<T>[] createGraphList(int size) {
        List<T>[] list = new List[size];
        for (int i = 0; i < size; i++) {
            list[i] = new ArrayList<>();
        }
        return list;
    }

    public static void main(String[] args) {
        new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            if (System.getProperty("ONLINE_JUDGE") == null) {
                time();
                memory();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    class People {
        long s, a, b;

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

    long S;
    long answer;

    List<People> removeBest(List<People> list) {
        ArrayDeque<People> deque = new ArrayDeque<>();
        deque.addAll(list);
        long sumS = 0;
        for (People p : list) {
            sumS += p.s;
        }
        long take = sumS / S * S;
        while (take > 0) {
            People cur = deque.poll();
            long takeNow = Math.min(take, cur.s);
            answer += takeNow * Math.max(cur.a, cur.b);
            take -= takeNow;
            if (cur.s == takeNow) continue;
            cur.s -= takeNow;
            deque.addFirst(cur);
        }
        list.clear();
        list.addAll(deque);
        return list;
    }

    void solve() throws IOException {
        int n = readInt();
        S = readInt();
        People[] peoples = new People[n];
        for (int i = 0; i < n; i++) {
            peoples[i] = new People(readInt(), readInt(), readInt());
        }
        List<People> preferFirst = new ArrayList<>();
        List<People> preferSecond = new ArrayList<>();
        for (People people : peoples) {
            if (people.a > people.b) {
                preferFirst.add(people);
            } else {
                preferSecond.add(people);
            }
        }
        Collections.sort(preferFirst, (o1, o2) -> {
            long lose1 = o1.a - o1.b;
            long lose2 = o2.a - o2.b;
            return -Long.compare(lose1, lose2);
        });

        Collections.sort(preferSecond, (o1, o2) -> {
            long lose1 = o1.b - o1.a;
            long lose2 = o2.b - o1.a;
            return -Long.compare(lose1, lose2);
        });

        preferFirst = removeBest(preferFirst);
        preferSecond = removeBest(preferSecond);

        long ostS = 0;
        for (People p : preferFirst) {
            ostS += p.s;
        }
        for (People p : preferSecond) {
            ostS += p.s;
        }
        if (ostS <= S) {
            long ifFirst = 0;
            long ifSecond = 0;
            for (People people : preferFirst) {
                ifFirst += people.a * people.s;
                ifSecond += people.b * people.s;
            }
            for (People people : preferSecond) {
                ifFirst += people.a * people.s;
                ifSecond += people.b * people.s;
            }
            answer += Math.max(ifFirst, ifSecond);
        } else {
            for (People people : preferFirst) {
                answer += people.s * people.a;
            }
            for (People people : preferSecond) {
                answer += people.s * people.b;
            }
        }
        out.println(answer);
    }

}


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

public class Template implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine(), " :");
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    int[] readIntArray(int size) throws IOException {
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    <T> List<T>[] createGraphList(int size) {
        List<T>[] list = new List[size];
        for (int i = 0; i < size; i++) {
            list[i] = new ArrayList<>();
        }
        return list;
    }

    public static void main(String[] args) {
        new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            if (System.getProperty("ONLINE_JUDGE") == null) {
                time();
                memory();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    class People {
        long s, a, b;

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

    long S;
    long answer;

    List<People> removeBest(List<People> list) {
        ArrayDeque<People> deque = new ArrayDeque<>();
        deque.addAll(list);
        long sumS = 0;
        for (People p : list) {
            sumS += p.s;
        }
        long take = sumS / S * S;
        while (take > 0) {
            People cur = deque.poll();
            long takeNow = Math.min(take, cur.s);
            answer += takeNow * Math.max(cur.a, cur.b);
            take -= takeNow;
            if (cur.s == takeNow) continue;
            cur.s -= takeNow;
            deque.addFirst(cur);
        }
        list.clear();
        list.addAll(deque);
        return list;
    }

    void solve() throws IOException {
        int n = readInt();
        S = readInt();
        People[] peoples = new People[n];
        for (int i = 0; i < n; i++) {
            peoples[i] = new People(readInt(), readInt(), readInt());
        }
        List<People> preferFirst = new ArrayList<>();
        List<People> preferSecond = new ArrayList<>();
        for (People people : peoples) {
            if (people.a > people.b) {
                preferFirst.add(people);
            } else {
                preferSecond.add(people);
            }
        }
        Collections.sort(preferFirst, (o1, o2) -> {
            long lose1 = o1.a - o1.b;
            long lose2 = o2.a - o2.b;
            return -Long.compare(lose1, lose2);
        });

        Collections.sort(preferSecond, (o1, o2) -> {
            long lose1 = o1.b - o1.a;
            long lose2 = o2.b - o2.a;
            return -Long.compare(lose1, lose2);
        });

        preferFirst = removeBest(preferFirst);
        preferSecond = removeBest(preferSecond);

        long ostS = 0;
        for (People p : preferFirst) {
            ostS += p.s;
        }
        for (People p : preferSecond) {
            ostS += p.s;
        }
        if (ostS <= S) {
            long ifFirst = 0;
            long ifSecond = 0;
            for (People people : preferFirst) {
                ifFirst += people.a * people.s;
                ifSecond += people.b * people.s;
            }
            for (People people : preferSecond) {
                ifFirst += people.a * people.s;
                ifSecond += people.b * people.s;
            }
            answer += Math.max(ifFirst, ifSecond);
        } else {
            for (People people : preferFirst) {
                answer += people.s * people.a;
            }
            for (People people : preferSecond) {
                answer += people.s * people.b;
            }
        }
        out.println(answer);
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

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

    static class TaskC {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            long S = in.nextLong();
            TaskC.Person[] p = new TaskC.Person[n];
            for (int i = 0; i < n; ++i) p[i] = new TaskC.Person();
            long need = 0;
            for (int i = 0; i < n; ++i) {
                p[i].s = in.nextInt();
                p[i].a = in.nextInt();
                p[i].b = in.nextInt();
                need += p[i].s;
            }
            long am = (need + S - 1) / S;
            long extra = am * S - need;
            p = Arrays.copyOf(p, n + 1);
            p[n] = new TaskC.Person();
            p[n].s = extra;
            ++n;
            Arrays.sort(p, new Comparator<TaskC.Person>() {

                public int compare(TaskC.Person o1, TaskC.Person o2) {
                    return (o1.a - o1.b) - (o2.a - o2.b);
                }
            });
            long res = 0;
            for (TaskC.Person pp : p) res += pp.s * pp.a;
            long cur = res;
            long tail = 0;
            for (TaskC.Person pp : p) {
                if (tail + pp.s < S) {
                    tail += pp.s;
                    cur += pp.s * (pp.b - pp.a);
                    continue;
                }
                long by = S - tail;
                tail += by;
                pp.s -= by;
                cur += (pp.b - pp.a) * by;
                res = Math.max(res, cur);
                long whole = pp.s / S;
                cur += (pp.b - pp.a) * S * whole;
                pp.s -= S * whole;
                res = Math.max(res, cur);
                tail = pp.s;
                cur += tail * (pp.b - pp.a);
            }
            if (tail != 0) throw new RuntimeException();
            out.println(res);
        }

        static class Person {
            long s;
            int a;
            int b;

        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int pnumChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        private int pread() {
            if (pnumChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= pnumChars) {
                curChar = 0;
                try {
                    pnumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (pnumChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = pread();
            while (isSpaceChar(c))
                c = pread();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = pread();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = pread();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = pread();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = pread();
            while (isSpaceChar(c))
                c = pread();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = pread();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = pread();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        private static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

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

    static class TaskB {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            long S = in.nextInt();
            TaskB.Person[] p = new TaskB.Person[n];
            for (int i = 0; i < n; ++i) p[i] = new TaskB.Person();
            long need = 0;
            for (int i = 0; i < n; ++i) {
                p[i].s = in.nextInt();
                p[i].a = in.nextInt();
                p[i].b = in.nextInt();
                need += p[i].s;
            }
            long am = (need + S - 1) / S;
            long extra = am * S - need;
            p = Arrays.copyOf(p, n + 1);
            p[n] = new TaskB.Person();
            p[n].s = extra;
            ++n;
            Arrays.sort(p, new Comparator<TaskB.Person>() {

                public int compare(TaskB.Person o1, TaskB.Person o2) {
                    return (o1.a - o1.b) - (o2.a - o2.b);
                }
            });
            long res = 0;
            for (TaskB.Person pp : p) res += pp.s * pp.a;
            long cur = res;
            long tail = 0;
            for (TaskB.Person pp : p) {
                if (tail + pp.s < S) {
                    tail += pp.s;
                    cur += pp.s * (pp.b - pp.a);
                    continue;
                }
                long by = S - tail;
                tail += by;
                pp.s -= by;
                cur += by * (pp.b - pp.a);
                res = Math.max(res, cur);
                long whole = pp.s / S;
                cur += (pp.b - pp.a) * S * whole;
                pp.s -= S * whole;
                res = Math.max(res, cur);
                tail = pp.s;
                cur += tail * (pp.b - pp.a);
            }
            if (tail != 0) throw new RuntimeException();
            out.println(res);
        }

        static class Person {
            int a;
            int b;
            long s;

        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int pnumChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        private int pread() {
            if (pnumChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= pnumChars) {
                curChar = 0;
                try {
                    pnumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (pnumChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = pread();
            while (isSpaceChar(c))
                c = pread();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = pread();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = pread();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = pread();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        private static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

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

    static class TaskC {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int S = in.nextInt();
            TaskC.Person[] p = new TaskC.Person[n];
            for (int i = 0; i < n; ++i) p[i] = new TaskC.Person();
            long need = 0;
            for (int i = 0; i < n; ++i) {
                p[i].s = in.nextInt();
                p[i].a = in.nextInt();
                p[i].b = in.nextInt();
                need += p[i].s;
            }
            long am = (need + S - 1) / S;
            long extra = am * S - need;
            p = Arrays.copyOf(p, n + 1);
            p[n] = new TaskC.Person();
            p[n].s = extra;
            ++n;
            Arrays.sort(p, new Comparator<TaskC.Person>() {

                public int compare(TaskC.Person o1, TaskC.Person o2) {
                    return (o1.a - o1.b) - (o2.a - o2.b);
                }
            });
            long res = 0;
            for (TaskC.Person pp : p) res += pp.s * pp.a;
            long cur = res;
            long tail = 0;
            for (TaskC.Person pp : p) {
                if (tail + pp.s < S) {
                    tail += pp.s;
                    cur += pp.s * (pp.b - pp.a);
                    continue;
                }
                long by = S - tail;
                tail += by;
                pp.s -= by;
                cur += (pp.b - pp.a) * by;
                res = Math.max(res, cur);
                long whole = pp.s / S;
                cur += (pp.b - pp.a) * S * whole;
                pp.s -= S * whole;
                res = Math.max(res, cur);
                tail = pp.s;
                cur += tail * (pp.b - pp.a);
            }
            if (tail != 0) throw new RuntimeException();
            out.println(res);
        }

        static class Person {
            long s;
            int a;
            int b;

        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int pnumChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        private int pread() {
            if (pnumChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= pnumChars) {
                curChar = 0;
                try {
                    pnumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (pnumChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = pread();
            while (isSpaceChar(c))
                c = pread();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = pread();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = pread();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = pread();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        private static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.Collections;
import java.io.InputStream;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        Pair<LongLongPair, ArrayList<IntIntPair>> simplify(ArrayList<IntIntPair> al, long slicesPerPizza) {
            long pleasure = 0;
            long totalSlicesWant = 0;
            for (IntIntPair iip : al) {
                totalSlicesWant += iip.first;
            }
            long easyPizzaAllocation = totalSlicesWant / slicesPerPizza;
            long easyPizzaSliceAllocation = easyPizzaAllocation * slicesPerPizza;
            Collections.sort(al, Comparator.comparing((IntIntPair iip) -> -iip.second)); // sort by largest improvement first
            ArrayList<IntIntPair> res = new ArrayList<>();
            for (IntIntPair iip : al) {
                long here = Math.min(easyPizzaSliceAllocation, iip.first);
                easyPizzaSliceAllocation -= here;
                pleasure += here * iip.second;
                if (here == 0) {
                    res.add(iip);
                } else if (here < iip.first) {
                    res.add(new IntIntPair((int) (iip.first - here), iip.second));
                }
            }
            return Pair.makePair(new LongLongPair(pleasure, easyPizzaAllocation), res);
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt(); // number of contestants
            long slicesPerPizza = in.readInt(); // slices per pizza
            long totalSlicesNeed = 0;
            long obviousPleasure = 0;
            ArrayList<IntIntPair>[] prefer = new ArrayList[2];
            for (int i = 0; i < prefer.length; i++) {
                prefer[i] = new ArrayList<>();
            }
            for (int idx = 0; idx < n; idx++) {
                int s = in.readInt();
                int aPleasure = in.readInt();
                int bPleasure = in.readInt();
                totalSlicesNeed += s;
                if (aPleasure == bPleasure) {
                    obviousPleasure += (long) aPleasure * s;
                } else if (aPleasure > bPleasure) {
                    prefer[0].add(new IntIntPair(s, aPleasure - bPleasure));
                    obviousPleasure += (long) bPleasure * s;
                } else if (bPleasure > aPleasure) {
                    prefer[1].add(new IntIntPair(s, bPleasure - aPleasure));
                    obviousPleasure += (long) aPleasure * s;
                } else throw new RuntimeException();
            }
            long totalPizzasNeed = (totalSlicesNeed + slicesPerPizza - 1) / slicesPerPizza;
            for (int i = 0; i < prefer.length; i++) {
                Pair<LongLongPair, ArrayList<IntIntPair>> get = simplify(prefer[i], slicesPerPizza);
                obviousPleasure += get.first.first;
                totalPizzasNeed -= get.first.second;
                prefer[i] = get.second;
            }
            long moar;
            if (totalPizzasNeed == 0) {
                moar = 0;
            } else if (totalPizzasNeed == 1) {
                moar = max(prefer[0], prefer[1]);
            } else {
                moar = sum(prefer[0]) + sum(prefer[1]);
            }
            out.printLine(obviousPleasure + moar);
        }

        private long sum(ArrayList<IntIntPair> a) {
            long res = 0;
            for (IntIntPair iip : a) {
                res += (long) iip.first * iip.second;
            }
            return res;
        }

        private long max(ArrayList<IntIntPair> a, ArrayList<IntIntPair> b) {
            long aSum = sum(a);
            long bSum = sum(b);
            return Math.max(aSum, bSum);
        }
    }
    static class LongLongPair implements Comparable<LongLongPair> {
        public final long first;
        public final long second;

        public LongLongPair(long first, long second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            LongLongPair pair = (LongLongPair) o;
            return first == pair.first && second == pair.second;
        }

        public int hashCode() {
            int result = Long.hashCode(first);
            result = 31 * result + Long.hashCode(second);
            return result;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        @SuppressWarnings ({"unchecked"})
        public int compareTo(LongLongPair o) {
            int value = Long.compare(first, o.first);
            if (value != 0) {
                return value;
            }
            return Long.compare(second, o.second);
        }
    }
    static class IntIntPair implements Comparable<IntIntPair> {
        public final int first;
        public final int second;

        public IntIntPair(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            IntIntPair pair = (IntIntPair) o;
            return first == pair.first && second == pair.second;
        }

        public int hashCode() {
            int result = first;
            result = 31 * result + second;
            return result;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        @SuppressWarnings ({"unchecked"})
        public int compareTo(IntIntPair o) {
            int value = Integer.compare(first, o.first);
            if (value != 0) {
                return value;
            }
            return Integer.compare(second, o.second);
        }
    }
    static class Pair<U, V> implements Comparable<Pair<U, V>> {
        public final U first;
        public final V second;

        public static <U, V> Pair<U, V> makePair(U first, V second) {
            return new Pair<U, V>(first, second);
        }

        private Pair(U first, V second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Pair pair = (Pair) o;
            return !(first != null ? !first.equals(pair.first) : pair.first != null) &&
                    !(second != null ? !second.equals(pair.second) : pair.second != null);
        }

        public int hashCode() {
            int result = first != null ? first.hashCode() : 0;
            result = 31 * result + (second != null ? second.hashCode() : 0);
            return result;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        @SuppressWarnings ({"unchecked"})
        public int compareTo(Pair<U, V> o) {
            int value = ((Comparable<U>) first).compareTo(o.first);
            if (value != 0) {
                return value;
            }
            return ((Comparable<V>) second).compareTo(o.second);
        }
    }
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(long i) {
            writer.println(i);
        }
    }
}


import java.io.*;
import java.security.SecureRandom;
import java.util.*;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        ConsoleIO io = new ConsoleIO(new InputStreamReader(System.in), new PrintWriter(System.out));
        //String test = "D-large";
        //ConsoleIO io = new ConsoleIO(new FileReader("D:\\Dropbox\\code\\practice\\jb\\src\\" + test + ".in"), new PrintWriter(new File("D:\\Dropbox\\code\\practice\\jb\\src\\" + test + "-out.txt")));

       new Main(io).solve();

        io.close();
    }

    ConsoleIO io;
    Main(ConsoleIO io) {
        this.io = io;
    }

    List<List<Integer>> gr = new ArrayList<>();
    long MOD = 1_000_000_007;

    public void solve() {
        int N = io.ri(), S = io.ri();
        Man[] data = new Man[N];
        for (int i = 0; i < N; i++) {
            int s = io.ri(), a = io.ri(), b = io.ri();
            data[i] = new Man(s, a, b);
        }

        Arrays.sort(data, (m1, m2) -> m1.dif - m2.dif);
        long res = 0;
        long allpieces = 0;
        long ap = 0;
        long bp = 0;
        for (int i = 0; i < N; i++) {
            Man m = data[i];
            res += ((long) m.b) * m.s;
            allpieces += data[i].s;
            if (m.a > m.b) ap += m.s;
            if (m.a < m.b) bp += m.s;
        }

        long aa = ap / S;
        long ar = ap % S;
        long bb = bp / S;
        long br = bp % S;

        long pizzas = (allpieces + S - 1) / S;
        long rem = pizzas * S - allpieces;

        pizzas -= (aa + bb);
        ap = aa*S;
        bp = bb*S;
        if (pizzas > 1 || pizzas == 0) {
            res = 0;
            for (int i = 0; i < N; i++) {
                Man m = data[i];
                res += Math.max(m.a, m.b) * m.s;
            }
        } else {
            res = 0;
            for (int i = 0; i < N; i++) {
                Man m = data[i];

                if (m.a < m.b) {
                    long v = Math.min(m.s, bp);
                    m.s -= v;
                    bp -= v;
                    res += v * m.b;
                }
            }
            for (int i = N-1; i >= 0; i--) {
                Man m = data[i];

                if (m.a > m.b) {
                    long v = Math.min(m.s, ap);
                    m.s -= v;
                    ap -= v;
                    res += v * m.a;
                }
            }

            long xa = 0;
            long xb = 0;
            for (int i = 0; i < N; i++) {
                Man m = data[i];
                xa += m.a * m.s;
                xb += m.b * m.s;
            }
            res += Math.max(xa, xb);
        }

        io.writeLine(res+"");
    }

    public class Man{
        public Man(int s, int a, int b){
            this.s = s;
            this.a = a;
            this.b = b;
            dif = a-b;
        }

        public long s;
        public long a;
        public long b;
        public int dif;
    }
}

class ConsoleIO {
    BufferedReader br;
    PrintWriter out;
    public ConsoleIO(Reader reader, PrintWriter writer){br = new BufferedReader(reader);out = writer;}
    public void flush(){this.out.flush();}
    public void close(){this.out.close();}
    public void writeLine(String s) {this.out.println(s);}
    public void writeInt(int a) {this.out.print(a);this.out.print(' ');}
    public void writeWord(String s){
        this.out.print(s);
    }
    public void writeIntArray(int[] a, int k, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < k; i++) {
            if (i > 0) sb.append(separator);
            sb.append(a[i]);
        }
        this.writeLine(sb.toString());
    }
    public int read(char[] buf, int len){try {return br.read(buf,0,len);}catch (Exception ex){ return -1; }}
    public String readLine() {try {return br.readLine();}catch (Exception ex){ return "";}}
    public long[] readLongArray() {
        String[]n=this.readLine().trim().split("\\s+");long[]r=new long[n.length];
        for(int i=0;i<n.length;i++)r[i]=Long.parseLong(n[i]);
        return r;
    }
    public int[] readIntArray() {
        String[]n=this.readLine().trim().split("\\s+");int[]r=new int[n.length];
        for(int i=0;i<n.length;i++)r[i]=Integer.parseInt(n[i]);
        return r;
    }
    public int[] readIntArray(int n) {
        int[] res = new int[n];
        char[] all = this.readLine().toCharArray();
        int cur = 0;boolean have = false;
        int k = 0;
        boolean neg = false;
        for(int i = 0;i<all.length;i++){
            if(all[i]>='0' && all[i]<='9'){
                cur = cur*10+all[i]-'0';
                have = true;
            }else if(all[i]=='-') {
                neg = true;
            }
            else if(have){
                res[k++] = neg?-cur:cur;
                cur = 0;
                have = false;
                neg = false;
            }
        }
        if(have)res[k++] = neg?-cur:cur;
        return res;
    }
    public int ri() {
        try {
            int r = 0;
            boolean start = false;
            boolean neg = false;
            while (true) {
                int c = br.read();
                if (c >= '0' && c <= '9') {
                    r = r * 10 + c - '0';
                    start = true;
                } else if (!start && c == '-') {
                    start = true;
                    neg = true;
                } else if (start || c == -1) return neg ? -r : r;
            }
        } catch (Exception ex) {
            return -1;
        }
    }
    public long readLong() {
        try {
            long r = 0;
            boolean start = false;
            boolean neg = false;
            while (true) {
                int c = br.read();
                if (c >= '0' && c <= '9') {
                    r = r * 10 + c - '0';
                    start = true;
                } else if (!start && c == '-') {
                    start = true;
                    neg = true;
                } else if (start || c == -1) return neg ? -r : r;
            }
        } catch (Exception ex) {
            return -1;
        }
    }
    public String readWord() {
        try {
            boolean start = false;
            StringBuilder sb = new StringBuilder();
            while (true) {
                int c = br.read();
                if (c!= ' ' && c!= '\r' && c!='\n' && c!='\t') {
                    sb.append((char)c);
                    start = true;
                } else if (start || c == -1) return sb.toString();
            }
        } catch (Exception ex) {
            return "";
        }
    }
    public char readSymbol() {
        try {
            while (true) {
                int c = br.read();
                if (c != ' ' && c != '\r' && c != '\n' && c != '\t') {
                    return (char) c;
                }
            }
        } catch (Exception ex) {
            return 0;
        }
    }
    //public char readChar(){try {return (char)br.read();}catch (Exception ex){ return 0; }}
}
class Pair {
    public Pair(int a, int b) {this.a = a;this.b = b;}
    public int a;
    public int b;
}
class PairLL {
    public PairLL(long a, long b) {this.a = a;this.b = b;}
    public long a;
    public long b;
}
class Triple {
    public Triple(int a, int b, int c) {this.a = a;this.b = b;this.c = c;}
    public int a;
    public int b;
    public int c;
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        final long[][] data = new long[n][3];
        int[] p = new int[n];
        int[] q = new int[n];
        PriorityQueue<Integer> P = new PriorityQueue<>((x, y) -> Long.compare(data[y][1], data[x][1]));
        PriorityQueue<Integer> Q = new PriorityQueue<>((x, y) -> Long.compare(data[y][2], data[x][2]));
        long total = 0;
        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();

            }
            total += data[i][0];
        }
        for (int i = 0; i < n; i++) {
            P.add(i);
            Q.add(i);
        }
        for (int i = 0; i < n; i++) {
            p[i] = P.poll();
            q[i] = Q.poll();
        }

        long need = total / s;
        if (need * s < total) {
            need++;
        }

        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, p, q, s * mid, s * (need - mid), n);
            long y = cal(data, p, q, s * (mid + 1), s * (need - mid - 1), n);
            //System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(long[][] data, int[] p, int[] q, long a, long b, int n) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        long[] hold = new long[n];

        int i = 0, j = 0;
        while (i < n && j < n) {
            if (data[p[i]][1] >= data[q[j]][2]) {
                int v = p[i++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, a);
                a -= need;
                hold[v] += need;
                re += data[v][1] * need;
            } else {
                int v = q[j++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, b);
                b -= need;
                hold[v] += need;
                re += data[v][2] * need;
            }
        }
        while (i < n) {
            int v = p[i++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, a);
            a -= need;
            hold[v] += need;
            re += data[v][1] * need;
        }
        while (j < n) {
            int v = q[j++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, b);
            b -= need;
            hold[v] += need;
            re += data[v][2] * need;
        }

        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

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

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        final long[][] data = new long[n][3];
        int[] p = new int[n];
        int[] q = new int[n];
        PriorityQueue<Integer> P = new PriorityQueue<>((x, y) -> Long.compare(data[y][1], data[x][1]));
        PriorityQueue<Integer> Q = new PriorityQueue<>((x, y) -> Long.compare(data[y][2], data[x][2]));
        long total = 0;
        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();

            }
            total += data[i][0];
        }
        for (int i = 0; i < n; i++) {
            P.add(i);
            Q.add(i);
        }
        for (int i = 0; i < n; i++) {
            p[i] = P.poll();
            q[i] = Q.poll();
        }

        long need = total / s;
        if (need * s < total) {
            need++;
        }

        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, p, q, s * mid, s * (need - mid), n);
            long y = cal(data, p, q, s * (mid + 1), s * (need - mid - 1), n);
            //System.out.println(x + " " + y + " " + mid);
            if (x >= y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(long[][] data, int[] p, int[] q, long a, long b, int n) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        long[] hold = new long[n];

        int i = 0, j = 0;
        while (i < n && j < n) {
            if (data[p[i]][1] >= data[q[j]][2]) {
                int v = p[i++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, a);
                a -= need;
                hold[v] += need;
                re += data[v][1] * need;
            } else {
                int v = q[j++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, b);
                b -= need;
                hold[v] += need;
                re += data[v][2] * need;
            }
        }
        while (i < n) {
            int v = p[i++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, a);
            a -= need;
            hold[v] += need;
            re += data[v][1] * need;
        }
        while (j < n) {
            int v = q[j++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, b);
            b -= need;
            hold[v] += need;
            re += data[v][2] * need;
        }

        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

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

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}