20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 10 4
0 1 4 2 7 0 10 0 5 8
 10 7
10 8 6 5 0 0 0 4 3 9
 10 1
8 7 0 2 0 10 0 0 3 5
 10 7
7 9 2 10 0 0 0 3 5 1
 10 2
10 0 9 0 0 4 2 6 8 0
 10 2
0 7 0 10 8 0 4 2 3 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 3
4
8
9
 1
5
6
10
 2
4
5
7
8
10
 1
2
6
7
 1
2
3
4
6
7
8
9
 4
5
6
7
8

#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string get() {
  char ch[1000010];
  scanf("%s", ch);
  return ch;
}
const int MAX_LG = 21;
const int maxn = 2e5 + 100;
const int base = 29;
const int mod = 1e9 + 7;
const int INF = 2e9 + 100;
int nxt[maxn];
int sz[maxn];
int deg[maxn];
bool mark[maxn];
bool dp[maxn];
bool dp2[maxn];
inline int root(int x) { return (nxt[x] == 0 ? x : nxt[x] = root(nxt[x])); }
inline void dfs(int v) {
  mark[v] = true;
  sz[v] = 1;
  if (nxt[v] == 0) return;
  int u = nxt[v];
  if (mark[u])
    sz[v] += sz[u];
  else {
    dfs(u);
    sz[v] += sz[u];
  }
}
int32_t main() {
  int n = in(), id = in();
  for (int i = 1; i <= n; i++) nxt[i] = in(), deg[nxt[i]]++;
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) dfs(i);
  }
  vector<int> vc;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 0 && root(i) != root(id)) vc.push_back(sz[i]);
  dp[0] = true;
  for (int i = 0; i < vc.size(); i++) {
    for (int j = 0; j + vc[i] <= 1000; j++) dp2[j + vc[i]] |= dp[j];
    for (int j = 0; j <= 1000; j++) dp[j] |= dp2[j], dp2[j] = false;
  }
  for (int x = 0; x <= 1000; x++) {
    if (dp[x]) {
      cout << x + sz[id] << "\n";
    }
  }
}
