#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, x;
int a[maxn], dlkjdslfkdj[maxn];
int ndep, comp;
vector<int> szs;
inline void dfs(int v, int dep, int num) {
  if (v == x) ndep = dep, comp = num;
  if (dlkjdslfkdj[v] != -1)
    dfs(dlkjdslfkdj[v], dep + 1, num);
  else
    szs[num] = dep + 1;
}
inline bool Less(int a, int b) { return szs[a] < szs[b]; }
int main() {
  scanf("%d%d", &n, &x);
  --x;
  for (int i = 0; i < n; i++) dlkjdslfkdj[i] = -1;
  vector<int> roots;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    --a[i];
    if (a[i] != -1)
      dlkjdslfkdj[a[i]] = i;
    else
      roots.push_back(i);
  }
  szs.resize(n);
  for (int i = 0; i < (int)roots.size(); i++) dfs(roots[i], 0, i);
  vector<int> s;
  for (int i = 0; i < (int)roots.size(); i++)
    if (i != comp) s.push_back(szs[i]);
  int maxval = 2000;
  vector<vector<int> > dp((int)s.size() + 1, vector<int>(maxval));
  dp[0][0] = 1;
  for (int i = 1; i <= (int)s.size(); i++)
    for (int val = 0; val < maxval; val++) {
      dp[i][val] = dp[i - 1][val];
      if (val >= s[i - 1]) dp[i][val] |= dp[i - 1][val - s[i - 1]];
    }
  vector<int> may;
  for (int i = 0; i < maxval; i++)
    if (dp[(int)s.size()][i]) may.push_back(i + ndep + 1);
  sort((may).begin(), (may).end());
  for (int i = 0; i < (int)may.size(); i++) printf("%d\n", may[i]);
  return 0;
}
