#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> p;
  p.reserve(n + 1);
  p.push_back(-1);
  p.push_back(-1);
  copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(p));
  vector<int> path;
  path.reserve(n);
  int cur = n;
  do {
    path.push_back(cur);
    cur = p[cur];
  } while (cur != -1);
  copy(path.rbegin(), path.rend(), ostream_iterator<int>(cout, " "));
}
