Getting Back Home

On a tree with home at node 1 and office at node n, find the smallest flashlight range d so that a random walk governed by visibility rules always reaches home within 10^9 steps.

Hard8TreeDFSGreedyGraphNo attempts yetTime limit2sMemory limit512 MB

Problem

Arman recently moved to a remote village in the countryside. The map of the village is a tree: exactly n1n-1 roads connect nn intersections, and every pair of intersections is joined by a sequence of roads.

Every morning Arman goes to his office, and late at night he gets back home. Nights are very dark and the village roads have no lights, so Arman has started to have trouble finding his way back home. The intersections carry no signs and he cannot tell them apart. His phone has no signal on the way either, so GPS is not possible. To solve the problem he decided to buy a flashlight. Flashlights have different integer beam distances, and a flashlight with a longer beam costs more. A flashlight with range dd reveals every intersection within distance at most dd of the current intersection. All roads of the village have the same length 1.

When Arman leaves the office towards home, he makes a decision at every intersection of the path he traverses.

  1. If he sees home, he just moves towards it.
  2. Assume he is at intersection uu. Let AA be the set of all roads incident to uu. Let BB be the empty set at the initial time, and {e}\{e\} otherwise, where ee is the road he just arrived at uu through. Let CC be the set of useless roads incident to uu. A road ee' incident to uu is called useless if all simple paths starting at uu and passing through ee' have length less than dd. If A(BC)A - (B \cup C) is not empty, one road of this set is chosen randomly. Otherwise the road ee is chosen again.

Arman does not mind walking a little bit longer as long as he knows that, regardless of his random choices, he will eventually reach home after passing through at most 10910^9 roads. He wants to purchase the cheapest flashlight for that purpose. Find the smallest beam range by which he is able to reach home.

Input

There are multiple test cases in the input. The first line of each test case contains an integer nn, the number of intersections in the village (2n300002 \le n \le 30000). Each of the next n1n-1 lines contains two integers aa and bb, meaning that there is a road between intersections aa and bb (1a,bn1 \le a, b \le n). Arman's home is at intersection 1 and his office is at intersection nn. The input terminates with a line containing 0, which should not be processed.

Output

For each test case, output a single line containing the minimum range dd of a flashlight that guarantees Arman can always reach home after passing through at most 10910^9 roads, regardless of his random choices. If Arman does not need any flashlight, print 0.