#include <bits/stdc++.h>
using namespace std;
int plus_plus;
vector<int> v;
vector<int> graf[1001];
int viz[1001];
int cine;
void dfs(int nod, int dist) {
  int vecini = 0;
  int vvv = 0;
  for (auto x : graf[nod]) {
    if (x == cine) {
      plus_plus = dist + 1;
      vvv = 1;
    } else {
      dfs(x, dist + 1);
      ++vecini;
    }
  }
  if (vecini == 0 && vvv == 0) v.push_back(dist);
}
int main() {
  int n;
  cin >> n >> cine;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    graf[x].push_back(i);
  }
  dfs(0, 0);
  viz[0] = 1;
  for (auto x : v)
    for (int i = 1000; i >= x; --i)
      if (viz[i - x] == 1) viz[i] = 1;
  for (int i = 0; i <= 1000; ++i)
    if (viz[i]) cout << i + plus_plus << '\n';
  return 0;
}
