Magical Portals

Time limit: 2 secMemory limit: 256 MBData Structures, Graph Theory, Dynamic Programming

Problem Statement

In the mystical realm of Algorithmland, a wise wizard has discovered a series of magical portals connecting different cities. There are NN cities numbered from 11 to NN, and exactly MM portals. Each portal allows travel in one direction only, taking a certain amount of time.

The wizard's quest is to travel quickly from city 11 to city NN. Your task is to determine the minimum amount of time required to achieve this journey. If no valid route exists from city 11 to city NN, output 1-1.

Input Format

  • The first line contains two space-separated integers, NN and MM, where NN is the number of cities and MM is the number of magical portals.
  • Each of the next MM lines contains three space-separated integers: uu, vv, and ww. The integer uu denotes the starting city, vv denotes the destination city of the portal, and ww is the time (in minutes) it takes to travel through that portal.

Output Format

  • Output a single integer: the minimum time required to travel from city 11 to city NN. If there is no possible route, output 1-1.

Examples

Input

4 4
1 2 5
2 3 10
1 4 100
3 4 5

Output

20

Input

3 1
2 3 4

Output

-1

Code Editor

Submit a solution. Authentication is required to create submissions.

Language
Theme
Source code is submitted to the judge for testing.