Pinball

The program installs the cheapest set of row devices so every falling ball lands in one bottom cell.

Medium7Dynamic programmingSegment treeNo attempts yetTime limit1sMemory limit512 MB

Problem

Sangsu likes the game Pinball. The rules are as follows.

The pinball board is a grid of square cells with M+2M+2 rows and NN columns. Row 1 is the top of the board and row M+2M+2 is the bottom. The cell in row ii and column jj is written (i,j)(i, j).

A ball appears on one of the cells in row 1 and falls straight down toward the bottom. A ball that appears at (1,i)(1, i) (1iN1 \le i \le N) passes through (j,i)(j, i) (2jM+12 \le j \le M+1) and lands on the bottom cell (M+2,i)(M+2, i). Sangsu scores a point when he hits the ball back.

Hitting the ball back is hard because the ball can land on any cell of the bottom row. Sangsu wants to install the devices described below on the board so that exactly one bottom cell is reachable.

There are MM devices, numbered 1 to MM, and each device is parallel to the rows of the board. Device ii (1iM1 \le i \le M) sits on the cells from (i+1,Ai)(i+1, A_i) to (i+1,Bi)(i+1, B_i), so it covers BiAi+1B_i - A_i + 1 cells. When a ball reaches a cell covered by this device, the ball is carried to (i+1,Ci)(i+1, C_i). The ball then falls straight down along column CiC_i. A single device never interacts with the same ball more than once.

Installing device ii costs DiD_i won. Sangsu picks some of the MM devices and installs them so that exactly one bottom cell is reachable, and he wants the total cost to be as small as possible.

The figure above shows a board with M=2M = 2 and N=4N = 4. A ball appears at the top cell (1,2)(1, 2). It moves down to (2,2)(2, 2), device 11 carries it to (2,3)(2, 3), and it finally lands on the bottom cell (4,3)(4, 3).

Given the size of the board and the information about the devices, write a program that computes the minimum cost of installing devices so that exactly one bottom cell is reachable.

Input

Read the following data from standard input.

The first line contains two integers MM and NN separated by a space. The board has M+2M+2 rows and NN columns, and there are MM devices.

Each of the next MM lines contains four integers AiA_i, BiB_i, CiC_i, DiD_i separated by spaces, where line ii (1iM1 \le i \le M) describes device ii. Device ii sits on the BiAi+1B_i - A_i + 1 cells from (i+1,Ai)(i+1, A_i) to (i+1,Bi)(i+1, B_i), and it carries a ball that reaches any cell it covers to (i+1,Ci)(i+1, C_i). Installing it costs DiD_i won.

Output

Print on the first line the minimum cost of installing devices so that exactly one bottom cell is reachable. If no such installation exists, print 1-1.

Constraints

  • 1M1000001 \le M \le 100\,000
  • 2N10000000002 \le N \le 1\,000\,000\,000
  • 1AiCiBiN1 \le A_i \le C_i \le B_i \le N (1iM1 \le i \le M)
  • 1Di10000000001 \le D_i \le 1\,000\,000\,000 (1iM1 \le i \le M)

Notes

The board and the device positions of the first example are shown below. The number written on each device is its installation cost.

Installing devices 22, 44 and 55 out of the five gives the board below.

Now, whichever top cell the ball appears on, it lands on the bottom cell (7,3)(7, 3). The three devices cost 2525 won in total. No installation cheaper than 2525 won leaves exactly one reachable bottom cell, so the answer is 2525.