Under construction

Still being built. Things may change

Treatment Project

Time limit2sMemory limit512 MB

Summary
Choose treatment projects with the smallest total cost so that no citizen stays infected. Each project treats a range of consecutive houses on one day.
Level

Hard8 of 10

Topics
Shortest path, Graph, Segment tree, Sorting
Solved
No attempts yet

Problem

There are NN houses in the JOI kingdom, numbered from 1 to NN. The houses lie on a line in numerical order. Each house has one citizen. The citizen living in house xx (1≤x≤N1 \le x \le N) is called citizen xx.

A new virus has infected every citizen. To deal with it, MM treatment projects are proposed. Project ii costs CiC_i. Performing project ii has the following effect.

In the evening of day TiT_i, every infected citizen xx with Li≤x≤RiL_i \le x \le R_i is treated.

The virus spreads to neighbors as follows. If citizen xx is infected in the morning of some day, then citizen x−1x-1 (if x≥2x \ge 2) and citizen x+1x+1 (if x≤N−1x \le N-1) become infected at noon of the same day. A treated citizen can be infected again.

Choose some of the projects so that, after all chosen projects are performed, no citizen is infected. Several projects may be performed on the same day.

Determine whether this condition can be satisfied. If it can, find the minimum total cost.

Input

The first line contains NN and MM. Each of the next MM lines contains TiT_i, LiL_i, RiR_i, and CiC_i.

N M
T1 L1 R1 C1
.
.
.
TM LM RM CM

Output

Print one line. If the condition cannot be satisfied, print -1. Otherwise, print the minimum total cost.

Constraints

  • 1≤N≤10000000001 \le N \le 1000000000
  • 1≤M≤1000001 \le M \le 100000
  • 1≤Ti≤10000000001 \le T_i \le 1000000000 (1≤i≤M1 \le i \le M)
  • 1≤Li≤Ri≤N1 \le L_i \le R_i \le N (1≤i≤M1 \le i \le M)
  • 1≤Ci≤10000000001 \le C_i \le 1000000000 (1≤i≤M1 \le i \le M)

Examples3

  1. Example 1

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

    Input
    10 5
    2 6 10 3
    1 1 5 5
    5 2 7 3
    8 6 10 4
    4 1 3 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    10 5
    1 5 10 4
    1 1 6 5
    1 4 8 3
    1 6 10 3
    1 1 3 1
    
    Expected output
    7