#ds805. Race
Race
Race
Problem Source
IOI 2011
Problem Description
You are given a tree with vertices, each edge carrying a positive weight. Find a simple path whose total weight equals , minimizing the number of edges on the path.
Input Format
The first line contains two integers ().
The next lines each contain three integers describing one undirected edge: its two endpoints and its weight. The weight is guaranteed to be a positive integer not exceeding .
Note that the vertices are numbered starting from .
Output Format
Output one integer: the minimum number of edges. If no such path exists, output -1.
Sample Input
4 3
0 1 1
1 2 2
1 3 4
Sample Output
2
Constraints
- Edge weights are positive integers
- Vertices are numbered from