Karel wants to register his robot for a robot contest. Registering takes a safety certificate for the robot and several forms. Karel starts at the entrance of the organizers' building, visits every office once in a prescribed order, and returns to the entrance. The hallways of the building contain no cycle, so exactly one path runs between any two offices.
Running a contest takes more than a problem statement and a model solution. The organizers also need test data that checks submitted programs, and rejecting inefficient programs takes the visiting order whose walk is as long as possible. Find that order.
The building is a tree with N vertices and one office in every vertex. A tree is a set of vertices joined by edges. Each edge joins two vertices, and exactly one path runs between any two vertices vi and vj. The length of a path is the number of edges you traverse when you walk from vi to vj, written dist(vi,vj).
The input holds several test cases. The first line of a test case holds the number of vertices of the tree, N (2≤N≤10000). The vertices are numbered from 0 to N−1. The i-th of the next N−1 lines holds an integer fi (0≤fi<i), meaning that the tree has an edge between vertex i and vertex fi.
One empty line follows each test case. The input ends at the end of the file. There are at most 100 test cases, and the sum of N over all test cases is at most 50000.
For each test case, print one line with N integers v1,…,vN separated by spaces. Each of 0,1,…,N−1 must appear exactly once, and the sum
∑i=1N−1dist(vi,vi+1)+dist(vN,v1)
must be as large as possible.
Several permutations usually reach that maximum. Print only the lexicographically smallest one. Comparing two permutations from the front, the one holding the smaller number at the first position where they differ is the lexicographically smaller one.