#include <bits/stdc++.h>
using namespace std;
vector<int> aa;
int pre[1005], nxt[1005], x, n, mark;
void dfs(int u, int blo, int ff) {
  if (u == x) {
    ff = blo;
    mark = blo;
  }
  if (!pre[u]) {
    if (!ff)
      aa.push_back(blo);
    else {
      mark = blo - ff + 1;
    }
  } else
    dfs(pre[u], blo + 1, ff);
}
bool f[1005];
int main() {
  int i;
  scanf("%d%d", &n, &x);
  for (i = 1; i <= n; ++i) {
    scanf("%d", &pre[i]);
    if (pre[i]) nxt[pre[i]] = i;
  }
  for (i = 1; i <= n; ++i)
    if (!nxt[i]) dfs(i, 1, 0);
  sort(aa.begin(), aa.end());
  int cnt = aa.size(), j;
  f[0] = 1;
  for (i = 0; i < cnt; ++i)
    for (j = n; j >= aa[i]; --j) f[j] = f[j] | f[j - aa[i]];
  for (i = 0; i <= n; ++i)
    if (f[i]) cout << i + mark << endl;
  return 0;
}
