1430_F. Realistic Gameplay_17139
Recently you've discovered a new shooter. They say it has realistic game mechanics.

Your character has a gun with magazine size equal to k and should exterminate n waves of monsters. The i-th wave consists of a_i monsters and happens from the l_i-th moment of time up to the r_i-th moments of time. All a_i monsters spawn at moment l_i and you have to exterminate all of them before the moment r_i ends (you can kill monsters right at moment r_i). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) — formally, the condition r_i ≤ l_{i + 1} holds. Take a look at the notes for the examples to understand the process better.

You are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly 1 unit of time.

One of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets.

You've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves.

Note that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine.

Input

The first line contains two integers n and k (1 ≤ n ≤ 2000; 1 ≤ k ≤ 10^9) — the number of waves and magazine size.

The next n lines contain descriptions of waves. The i-th line contains three integers l_i, r_i and a_i (1 ≤ l_i ≤ r_i ≤ 10^9; 1 ≤ a_i ≤ 10^9) — the period of time when the i-th wave happens and the number of monsters in it.

It's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. r_i ≤ l_{i + 1}.

Output

If there is no way to clear all waves, print -1. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves.

Examples

Input


2 3
2 3 6
3 4 3


Output


9


Input


2 5
3 7 11
10 12 15


Output


30


Input


5 42
42 42 42
42 43 42
43 44 42
44 45 42
45 45 1


Output


-1


Input


1 10
100 111 1


Output


1

Note

In the first example: 

  * At the moment 2, the first wave occurs and 6 monsters spawn. You kill 3 monsters and start reloading. 
  * At the moment 3, the second wave occurs and 3 more monsters spawn. You kill remaining 3 monsters from the first wave and start reloading. 
  * At the moment 4, you kill remaining 3 monsters from the second wave. 

In total, you'll spend 9 bullets.

In the second example: 

  * At moment 3, the first wave occurs and 11 monsters spawn. You kill 5 monsters and start reloading. 
  * At moment 4, you kill 5 more monsters and start reloading. 
  * At moment 5, you kill the last monster and start reloading throwing away old magazine with 4 bullets. 
  * At moment 10, the second wave occurs and 15 monsters spawn. You kill 5 monsters and start reloading. 
  * At moment 11, you kill 5 more monsters and start reloading. 
  * At moment 12, you kill last 5 monsters. 

In total, you'll spend 30 bullets.
import java.util.*;
import java.io.*;

public class F1430 {

	static int n, k, l[], r[], a[];
	static long ps[], dp[][];

	public static long getPS(int l, int r) {
		return (r == -1 ? 0 : ps[r]) - (l == 0 ? 0 : ps[l - 1]);
	}

	public static long dp(int idx, int lastReload) {
		if (dp[idx][lastReload] != -1) {
			return dp[idx][lastReload];
		}
		int len = r[idx] - l[idx];
		long bulletsNow = lastReload == idx ? k : (k - getPS(lastReload, idx - 1) % k) % k;
		long ans = (long) 1e18;
		if (idx == n - 1) {
			if (bulletsNow + 1l * len * k >= a[idx])
				return dp[idx][lastReload] = 0;
			else
				return dp[idx][lastReload] = (long) 1e18;
		}
		if (r[idx] == l[idx + 1]) {
			if (bulletsNow + 1l * (len - 1) * k >= a[idx]) {
				ans = Math.min(ans, dp(idx + 1, lastReload));
				ans = Math.min(ans, (bulletsNow + 1l * (len - 1) * k - a[idx]) % k + dp(idx + 1, idx + 1));
			} else if (bulletsNow + 1l * (len) * k >= a[idx]) {
				ans = Math.min(ans, dp(idx + 1, lastReload));
			}
		} else {
			if (bulletsNow + 1l * len * k >= a[idx]) {
				ans = Math.min(ans, dp(idx + 1, lastReload));
				ans = Math.min(ans, (bulletsNow + 1l * len * k - a[idx]) % k + dp(idx + 1, idx + 1));
			}
		}
		return dp[idx][lastReload] = ans;
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		n = sc.nextInt();
		k = sc.nextInt();
		l = new int[n];
		r = new int[n];
		a = new int[n];
		ps = new long[n];
		for (int i = 0; i < n; i++) {
			l[i] = sc.nextInt();
			r[i] = sc.nextInt();
			a[i] = sc.nextInt();
			if (i == 0) {
				ps[i] = a[i];
			} else {
				ps[i] = a[i] + ps[i - 1];
			}
		}
		dp = new long[n][n];
		for (long[] x : dp) {
			Arrays.fill(x, -1);
		}
		long wasted = dp(0, 0);
		if (wasted == (long) 1e18) {
			pw.println(-1);
		} else {
			pw.println(wasted + ps[n - 1]);
		}
		pw.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

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

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}
