This page is still under construction.

Parts of this page are still being built. What you see may change.

Tuna

Time limit1sMemory limit64 MB

Summary
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.
Level

Easy1 of 10

Topics
Implementation, Math
Solved
No attempts yet

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 (1≤N≤201 \le N \le 20), the number of tunas.

The second line contains the integer XX (1≤X≤101 \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 (1≤P1,P2≤1001 \le P_1, P_2 \le 100).
  • One line with two integers P1P_1 and P2P_2 (1≤P1,P2≤1001 \le P_1, P_2 \le 100), followed by a second line with the integer P3P_3 (1≤P3≤1001 \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.

Examples3

  1. Example 1

    Input
    5
    2
    3 4
    2 1
    5 3
    4 4
    4 2
    
    Expected output
    19
    
  2. Example 2

    Input
    4
    2
    3 5
    2 8
    4
    6 5
    6 3
    7
    
    Expected output
    22
    
  3. Example 3

    Input
    3
    10
    20 50
    30
    20 40
    50
    70 20
    10
    
    Expected output
    90