시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 (추가 시간 없음) 1024 MB67291648.485%

문제

You are the coordinator for a competitive programming club. You need to hire some teachers to give lectures. There are a fixed number of lectures that need to be given this year. Additionally, there are a limited number of teachers that are willing to give lectures. Each teacher can teach up to three lectures, but not all the teachers need to teach a lecture, i.e., a teacher could teach 0, 1, 2, or 3 lectures. Each teacher charges a different amount depending on the number of lectures they give.

The money not spent will be used to fly the team to other contests, so you want to spend as little money as possible hiring enough teachers to give all the lectures.

Given the number of lectures to teach and how much each teacher charges for giving the lectures, determine the least amount of money necessary such that all the lectures will be taught.

입력

The first input line contains two integers, L and T (1 ≤ L ≤ 5000, L/3 ≤ T ≤ L), representing (respectively) the number of lectures and the number of teachers. Each of the following T input lines contains three integers, the i th of which contains ai1, ai2, and ai3 (0 < ai1 < ai2 < ai3 ≤ 100,000), representing (respectively) how much the i th teacher charges to give 1, 2, and 3 lectures.

출력

Print on a single line by itself a single positive integer: the least cost for paying the teachers to cover all L lectures. Assume that there are enough teachers to cover all the lectures.

예제 입력 1

4 3
8 10 20
10 20 30
11 17 25

예제 출력 1

27

예제 입력 2

6 2
10 20 25
30 35 37

예제 출력 2

62

예제 입력 3

5 2
10 20 25
30 35 37

예제 출력 3

57

노트

For the first Sample Input, the first teacher can give two lectures and the third teacher can give two lectures, so the total cost is 10 + 17 = 27.

For the third Sample Input, the first teacher can give two lectures and the second teacher can give three lectures, so the total cost is 20 + 37 = 57.