Under construction

Still being built. Things may change

Patrol

Time limit1sMemory limit64 MB

Summary
Build K (1 or 2) unit-length shortcuts in a tree so that the shortest closed walk from village 1 covering every edge exactly as required is minimized.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, DFS, Greedy
Solved
No attempts yet

Problem

There are NN villages numbered 11 through NN, joined by N−1N-1 roads that connect all of them. Each road links exactly two villages, and from any village you can reach every other village using only these roads (so the roads form a tree). Each road has length 11.

To keep everyone safe, a patrol must travel along every road every day. The police station is in village 11, so each day the patrol starts at village 11 and must finally return to village 11. Note that to finish a day's duty the patrol has to travel along each road exactly twice, so on a tree the total distance is 2(N−1)2(N-1); for example, on a certain tree of 88 villages this distance is 1414.

To reduce the total distance the patrol travels, KK new shortcuts will be built between the villages. Each shortcut is a new road of length 11 connecting two villages. Two shortcuts may start at the same village, and a shortcut may even be a loop, that is, connect a village to itself. Because the budget is limited, KK is 11 or 22. So that no money is wasted, the patrol must travel along each shortcut exactly once per day.

On the tree of 88 villages above, building one well-chosen shortcut lowers the patrol's total distance to 1111, and building two shortcuts can lower it to 1010. A poorly chosen pair of shortcuts, however, can even make the total larger than 1414 (for instance 1515), precisely because the patrol is required to travel each shortcut exactly once.

Given the roads and the number KK of shortcuts to build, decide where to build the shortcuts so that the total distance the patrol travels each day is as small as possible, and output that minimum distance.

Input

The first line contains two integers NN (3≤N≤1000003 \le N \le 100000) and KK (1≤K≤21 \le K \le 2).

Each of the next N−1N-1 lines contains two integers AA and BB (1≤A,B≤N1 \le A, B \le N), meaning there is a road connecting villages AA and BB.

Output

Output a single integer: the minimum possible total distance the patrol must travel each day after building the KK shortcuts optimally.

Examples1

  1. Example 1

    Input
    8 1
    1 2
    3 1
    3 4
    5 3
    7 5
    8 5
    5 6
    
    Expected output
    11