PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000035. Trồng cây theo yêu cầu (Planting Trees)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trồng cây theo yêu cầu (Planting Trees)

Planting Trees

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn consecutive planting spots, at most one tree in each. Each resident requires at least tit_i planted trees among positions bib_i through eie_i. Intervals may overlap. Minimize the number of trees.

Input

The first line contains nn, the second line hh, followed by hh lines bi,ei,tib_i,e_i,t_i.

Output

Print the minimum number of trees.

Subtasks

  • Subtask 1 (20 points): $1\le n\le40; 1\le h\le20; 1\le b_i\le e_i\le n; 1\le t_i\le e_i-b_i+1$.
  • Subtask 2 (30 points): $1\le n\le3000; 1\le h\le800; 1\le b_i\le e_i\le n; 1\le t_i\le e_i-b_i+1$.
  • Subtask 3 (50 points): $1\le n\le30000; 1\le h\le5000; 1\le b_i\le e_i\le n; 1\le t_i\le e_i-b_i+1$.

Examples

Input

5
2
1 3 2
3 5 2

Output

3

Explanation

Plant trees at positions 2,3,52,3,5. Each of the two resident intervals then has at least two trees. Because the intervals share only position 33, two trees cannot meet both demands.