Alice's Zigzag Bracelet
Bob strings n beads, numbered 1 to n, onto a single chain to make a bracelet for Alice. He
hands her a note describing it: the id of the head bead, and for every bead, the id of the bead
wired directly after it (or 0 if it is the last bead of the chain). The beads are not numbered in
the order they appear on the chain, so Alice has to follow the pointers from the head to actually see
the bracelet.
Reading the note from the head gives the chain order L_0 (the head), L_1, L_2, ..., L_{n-1}
(the tail) — every bead appears exactly once. Alice thinks the bracelet would look nicer rewoven
zigzag: starting at one end and alternating with the other end, working inward. She wants to re-string
the beads in the order
L_0, L_{n-1}, L_1, L_{n-2}, L_2, L_{n-3}, ...
until every bead has been placed once. Help Alice find the new next-bead pointer for every bead.
Input
The first line contains one integer n (1 ≤ n ≤ 2·10^5), the number of beads.
The second line contains one integer h (1 ≤ h ≤ n), the id of the head bead.
Each of the next n lines contains one integer: the i-th of these lines (1-indexed) gives next_i
(0 ≤ next_i ≤ n), the id of the bead that follows bead i in Bob's chain, or 0 if bead i is
the last bead of the chain.
It is guaranteed that the note describes a single chain through all n beads: starting at h and
repeatedly following next visits every bead exactly once and ends at 0.
Output
Print n lines. The i-th line (1-indexed) must contain the id of the bead that follows bead i in
Alice's new arrangement, or 0 if bead i is last in the new arrangement.
Notes
In the first example Bob's chain, read from the head 3, is 3, 5, 1, 4, 2. Alice's new order is
3, 2, 5, 4, 1 (first bead, then last, then second, then second-to-last, then the one bead left in
the middle). So bead 3 is now followed by 2, bead 2 by 5, bead 5 by 4, bead 4 by 1,
and bead 1 is last.
In the second example there is only one bead, so it has no bead after it.