This page is still under construction.

Parts of this page are still being built. What you see may change.

Race

Time limit3sMemory limit256 MB

Summary
Given a weighted tree, find a path of total length exactly K that uses the fewest edges, or report -1 if none exists.
Level

Hard8 of 10

Topics
Tree, Divide and conquer, DFS, Binary search
Solved
No attempts yet

Problem

For the IOR racing competition, you need to find the most suitable race course.

A region has NN cities connected by N−1N-1 highways. Each highway is bidirectional, connects two distinct cities, and has an integer length measured in kilometers. Any two cities are connected by exactly one path. In other words, the cities and highways form a tree.

A race course is a path between a distinct start city and end city whose total length is exactly KK kilometers. To avoid collisions, no highway may be used more than once (so no city is visited more than once either). Because the path between any two cities in a tree is unique, this condition is automatically satisfied.

To reduce traffic congestion, among all paths whose total length is exactly KK, you must find the one that uses the fewest highways (edges).

Cities are numbered from 00 to N−1N-1. The city numbers joined by a highway are between 00 and N−1N-1, and each highway length is an integer between 0 and 1,000,000. All cities are connected.

Print the number of highways in a path of total length exactly KK that uses the fewest highways. If no such path exists, print −1-1.

Input

The first line contains the number of cities NN and the race course length KK, separated by a space.

Each of the next N−1N-1 lines describes one highway with three integers uu, vv, and ww, meaning a highway of length ww connects city uu and city vv.

Output

Print, on a single line, the number of highways in a shortest (fewest-edge) path whose total length is exactly KK. If no such path exists, print −1-1.

Examples3

  1. Example 1

    Input
    4 3
    0 1 1
    1 2 2
    1 3 4
    
    Expected output
    2
    
  2. Example 2

    Input
    3 3
    0 1 1
    1 2 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    11 12
    0 1 3
    0 2 4
    2 3 5
    3 4 4
    4 5 6
    0 6 3
    6 7 2
    6 8 5
    8 9 6
    8 10 7
    
    Expected output
    2