QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#3733. Subway

Statistics

Bobo lives in the big city of ICPCCamp.

ICPCCamp has $n$ subway stations, numbered $1, 2, \dots, n$. There are $m$ bidirectional subway lines connecting the $n$ stations. The $i$-th subway line belongs to route $c_i$ and connects stations $a_i$ and $b_i$. Traveling between $a_i$ and $b_i$ takes $t_i$ minutes in either direction (i.e., it takes $t_i$ minutes to go from $a_i$ to $b_i$, and $t_i$ minutes to go from $b_i$ to $a_i$).

As everyone knows, transferring between lines is troublesome. If you arrive at a subway station $s$ via the $i$-th subway line and depart from station $s$ via the $j$-th subway line, you must spend an additional $|c_i - c_j|$ minutes. Note that transfers can only occur within subway stations.

Bobo wants to know the minimum time required to travel from subway station $1$ to subway station $n$.

Input

The input contains no more than $20$ test cases.

The first line of each test case contains two integers $n, m$ ($2 \leq n \leq 10^5, 1 \leq m \leq 10^5$).

Each of the next $m$ lines contains four integers $a_i, b_i, c_i, t_i$ ($1 \leq a_i, b_i, c_i \leq n, 1 \leq t_i \leq 10^9$).

It is guaranteed that there exists a path from subway station $1$ to $n$ (not necessarily direct).

Output

For each test case, output a single integer representing the required minimum time.

Examples

Input 1

3 3
1 2 1 1
2 3 2 1
1 3 1 1
3 3
1 2 1 1
2 3 2 1
1 3 1 10
3 2
1 2 1 1
2 3 1 1

Output 1

1
3
2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.