Tuna

For each tuna, take the larger of two estimates when they differ by at most X, otherwise take a third estimate, and print the sum.

Easy1ImplementationMathNo attempts yetTime limit1sMemory limit64 MB

Problem

Fisherman Šime caught NN tunas last night. Using a special app, he offered them for sale to a well-known Japanese company that buys quality fish. How does the app estimate the value, or price, of a tuna?

From a photo of the tuna, the app returns two estimates, P1P_1 and P2P_2. If the difference between the estimates is less than or equal to XX, the higher estimate is taken. If the difference is strictly greater than XX, the app returns a third estimate P3P_3, and that estimate is taken as the final value of the tuna.

Given the estimates for each of the NN tunas (sometimes two of them, sometimes three), write a program that outputs the total value of the caught tunas.

Input

The first line contains the integer NN (1N201 \le N \le 20), the number of tunas.

The second line contains the integer XX (1X101 \le X \le 10) described above.

Then NN blocks follow, one per tuna, each in one of these two forms:

  • One line with two integers P1P_1 and P2P_2 (1P1,P21001 \le P_1, P_2 \le 100).
  • One line with two integers P1P_1 and P2P_2 (1P1,P21001 \le P_1, P_2 \le 100), followed by a second line with the integer P3P_3 (1P31001 \le P_3 \le 100).

The second form is used when the difference between P1P_1 and P2P_2 is greater than XX.

Output

Output the total value of the caught tunas on a single line.