Under construction

Still being built. Things may change

Backpack Buddies

Time limit1sMemory limit512 MB

Summary
Find how many hours Dr. Knight waits at cabin n-1 when Mr. Day must end each day at a cabin, while Dr. Knight can stop anywhere after at most 12 hours.
Level

Medium6 of 10

Topics
Graph, Shortest path
Solved
No attempts yet

문제

The prudent Mr. Day and the adventurous Dr. Knight have decided to be backpack buddies and go trekking in the gorgeous Norwegian mountains. Of course they will use the trails marked by the Norwegian Trekking Association (DNT), like any sane trekkers would. Every such trail starts and ends at a cabin where it is possible to stay overnight.

Mr. Day likes sleeping comfortably, so he immediately suggested that they sleep in a cabin every night.

Dr. Knight disagreed. She argued that they would arrive at their destination much sooner if they walked as far as possible each day, then slept in tents between two cabins when needed. Mr. Day agreed that reaching the destination quickly was desirable, but he was not willing to give up the comfort of a proper bed.

After a fierce argument, they decided to split up and each follow their own strategy. Assuming they walk at the same pace, how many hours will Dr. Knight need to wait at the final destination before Mr. Day shows up?

입력

The first line contains two integers, nn (1≤n≤1000001 \le n \le 100000), the number of cabins, and mm (1≤m≤1000001 \le m \le 100000), the number of trails. The next mm lines each describe a trail. The ii-th of these lines contains three integers uiu_i, viv_i, and did_i (0≤ui,vi<n0 \le u_i, v_i < n, 0≤di≤120 \le d_i \le 12), meaning there is a trail between cabins uiu_i and viv_i that takes exactly did_i hours to traverse. There can be more than one trail between two cabins, and a trail can lead back to the cabin it started from.

Mr. Day and Dr. Knight start in cabin 0, and their destination is cabin n−1n-1. Each day they start walking at 08:00 and walk non-stop for at most 12 hours before settling down for the night.

출력

Output a single integer, the number of hours Dr. Knight needs to wait at the destination before Mr. Day shows up.

Examples2

  1. Example 1

    Input
    5 6
    0 1 2
    0 3 8
    1 2 11
    2 3 5
    2 4 2
    4 3 9
    
    Expected output
    4
    
  2. Example 2

    Input
    3 2
    0 1 2
    1 2 12
    
    Expected output
    10