#P51434. [JOI 2021 Final] Robot

[JOI 2021 Final] Robot

Description

There are NN crossings in the IOI town, numbered from 11 to NN. There are MM roads, numbered from 11 to MM.

Each road connects two different crossings in both directions. The road ii (1iM1 \leq i \leq M) connects the crossing AiA_i and the crossing BiB_i. No two different roads connect the same pair of crossings. Each of the roads has a color, which is described as an integer between 11 and MM, inclusive. Currently, the color of the road ii is CiC_i. More than one road may have the same color.

The JOI Co., Ltd. developed a robot moving around the crossings of the IOI town. Whenever you tell a color to the robot, the robot will find the road with that color, and then the robot will pass through it and moves to the adjacent crossing. However, if there are more than one roads with the told color connected to the current crossing of the robot, it cannot decide which road it should pass through, and will halt.

The robot is currently in the crossing 11. Your task is to move the robot to the crossing NN by telling colors to it. However, it is not always true that the robot can be moved to the crossing NN. You may change the colors of some of the roads in advance so that the robot can be moved to the crossing NN. It costs PiP_i yen to change the color of the road ii (1iM1 \leq i \leq M) to any color between 11 and MM, inclusive.

Write a program which, given the information of the crossings and the roads, calculates the minimum total cost. However, if it is impossible to move the robot to the crossing N even if you change the colors of the roads, output -1 instead.

Input

Read the following data from the standard input. Given values are all integers.

N MN\ M
A1 B1 C1 P1A_1\ B_1\ C_1\ P_1
\vdots
AM BM CM PMA_M\ B_M\ C_M\ P_M

Output

Write one line to the standard output. The output should contain the minimum total cost. However, if it is impossible to move the robot to the crossing NN even if you change the colors of the roads, output -1 instead.

Sample 1

4 6
1 4 4 4
3 4 1 3
1 3 4 4
2 4 3 1
2 3 3 2
1 2 4 2
3

You can change the color of the road 44 from color 33 to color 44 at the cost of 11 yen. You can change the color of the road 66 from color 44 to color 22 at the cost of 22 yen. The total cost is 33 yen.

After that, you tell the color 22 to the robot, then it moves from the crossing 11 to the crossing 22. And, you tell the color 44 to the robot, then it moves to the crossing 44.

It is impossible to pay less than 33 yen so that the robot can be moved to the crossing 44. Hence output 33.

5 2
1 4 1 2
3 5 1 4
-1

It is impossible to move the robot to the crossing 55 even if you change the colors of the roads. Hence output -1.

5 7
2 3 7 1
1 4 5 1
4 5 3 1
3 4 7 1
2 4 3 1
3 5 6 1
1 2 5 1

1

This sample input satisfies the constraints of the subtask 22.

Constraints

  • 2N1052 \leq N \leq 10^5.
  • 1M2×1051 \leq M \leq 2 \times 10^5.
  • 1Ai<BiN1 \leq A_i < B_i \leq N (1iM1 \leq i \leq M).
  • (Ai,Bi)(Aj,bj)(A_i, B_i) \neq (A_j, b_j) (1i<jM1 \leq i < j \leq M).
  • 1CiM1 \leq C_i \leq M (1iM1 \leq i \leq M).
  • 1Pi1091 \leq P_i \leq 10^9 (1iM1 \leq i \leq M).

Subtasks

  1. (3434 points) N1000N \leq 1000, M2000M \leq 2000.
  2. (2424 points) Pi=1P_i = 1 (1iM1 \leq i \leq M).
  3. (4242 points) No additional constraints.