#include using namespace std; typedef long long ll; typedef unsigned long long ull; struct gate { int id; vector children; int leftmostOpenGate = 0; void incOpened() { leftmostOpenGate++; leftmostOpenGate %= children.size(); } }; void drop(vector & gates, int id) { if (gates[id].children.size() == 0) { cout << id << endl; return; } int nextId = gates[id].children[gates[id].leftmostOpenGate]; gates[id].incOpened(); drop(gates, nextId); } int main(void) { ios_base::sync_with_stdio(false); int n, q; cin >> n >> q; vector allGates; allGates.push_back({0, {}, 0}); for(int i = 1; i < n; i++) { allGates.push_back({i, {}, 0}); int pred; cin >> pred; allGates[pred].children.push_back(i); } for(int i = 0; i < q; i++) { drop(allGates, 0); } return 0; }