#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int nxt[N];
int pos[N];
int in[N];
int cnt[N];
int n, st;
int dp[N];
int main() {
  while (cin >> n >> st) {
    memset(in, 0, sizeof(in));
    memset(nxt, -1, sizeof(nxt));
    int a;
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a);
      if (a == 0) continue;
      nxt[a] = i;
      ++in[i];
    }
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;
    for (int i = 1; i <= n; ++i) {
      if (in[i] == 0) {
        bool has = false;
        int cur = i;
        if (cur == st) has = true;
        int cnt = 0;
        pos[cur] = ++cnt;
        while (nxt[cur] != -1) {
          cur = nxt[cur];
          pos[cur] = ++cnt;
          if (cur == st) has = true;
        }
        if (!has) {
          for (int i = N - 1; i >= 0; --i) {
            if (i + cnt < N && dp[i]) dp[i + cnt] = 1;
          }
        }
      }
    }
    for (int i = 0; i + pos[st] < N; ++i)
      if (dp[i]) printf("%d ", i + pos[st]);
    puts("");
  }
}
