Fireworks display is one of the most exciting events in a festival. It is important in a fireworks display that every explosive connected to a switch by fuses explodes simultaneously at a planned time. Since the explosives used in the fireworks are very dangerous, they are set up far apart from the switch and are connected to the switch by some number of fuses. To connect several explosions to the switch fuses are connected as if edges are connected in a tree as shown in [Figure 1]. The spark starts from the switch, and moves along the fuses. When a spark reaches a junction, the spark spreads to all the fuses connected to the junction. The speed at which the sparks move is constant. [Figure 1] shows how six explosives
Hyunmin, who participated in the fireworks display, made a connection layout. Unfortunately, in his layout, the explosives may not explode at the same time. We want to have all explosives explode at the same time by changing the lengths of some fuses. For example, to have all the explosives in [Figure 1] explode at time
The cost of changing the length of a fuse is equal to the absolute value of difference in fuse length. For example, if the layout shown in [Figure 1] changes to the layout on the left in [Figure 2], the total cost is
The length of a fuse can be fully reduced to
Given a connection layout, you are to make a program which adjusts the fuse lengths so that all the explosives explode at the same time with minimum cost.
Input Specification
All input values are positive integers. Let
The input is given as follows:
Output Specification
Print the minimum cost to adjust the lengths of fuses to have all the explosives explode at the same time.
Sample Input
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
Sample Output
5
Comments