跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F1570. Flights for Regular Customers

  • ID: 1576
  • 传统题
  • 2000ms
  • 256MiB
  • 尝试: 0
  • AC · 已通过: 0
  • 教学难度:挑战
  • 上传者:
  • 动态规划
  • 矩阵运算
  • 数学
  • 挑战
  • 难度
  • 共享题库
  • Codeforces
  • 英文题面
  • 题目来源
  • 题面语言

Flights for Regular Customers

题目描述

D. Flights for Regular Customers
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the country there are exactly n cities numbered with positive integers from 1 to n. In each city there is an airport is located.

Also, there is the only one airline, which makes m flights. Unfortunately, to use them, you need to be a regular customer of this company, namely, you have the opportunity to enjoy flight i from city ai to city bi only if you have already made at least di flights before that.

Please note that flight i flies exactly from city ai to city bi. It can not be used to fly from city bi to city ai. An interesting fact is that there may possibly be recreational flights with a beautiful view of the sky, which begin and end in the same city.

You need to get from city 1 to city n. Unfortunately, you've never traveled by plane before. What minimum number of flights you have to perform in order to get to city n?

Note that the same flight can be used multiple times.

Input

The first line contains two integers, n and m (2≤n≤150, 1≤m≤150) − the number of cities in the country and the number of flights the company provides.

Next m lines contain numbers ai, bi, di (1≤ai,bi≤n, 0≤di≤109), representing flight number i from city ai to city bi, accessible to only the clients who have made at least di flights.

Output

Print "Impossible" (without the quotes), if it is impossible to get from city 1 to city n using the airways.

But if there is at least one way, print a single integer − the minimum number of flights you need to make to get to the destination point.

Examples
Input
3 2
1 2 0
2 3 1
Output
2
Input
2 1
1 2 100500
Output
Impossible
Input
3 3
2 1 0
2 3 6
1 2 0
Output
8

题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 1091 题。保留原作者与原赛事署名。