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;
int maz[1005][1005];
bool vist[5005];
vector<int> vec, ans;
int block[1005], f[1500];
int n, x;
int pos, numb;
bool flag;
int tot;
void dfs(int bb, int cur) {
  vist[cur] = 1;
  tot++;
  if (cur == x) {
    pos = tot;
    numb = bb;
    flag = 1;
  }
  for (int i = (1); i <= (n); i++) {
    if (!vist[i] && maz[cur][i]) {
      dfs(bb, i);
    }
  }
}
int main() {
  memset(maz, 0, sizeof(maz));
  memset(vist, 0, sizeof(vist));
  memset(block, 0, sizeof(block));
  vec.clear();
  int a;
  scanf("%d%d", &n, &x);
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &a);
    if (a == 0)
      vec.push_back(i);
    else
      maz[a][i] = 1;
  }
  int sz = vec.size();
  for (int i = 0; i < sz; i++) {
    flag = 0;
    tot = 0;
    dfs(i, vec[i]);
    if (!flag) block[i] = tot;
  }
  sort(block, block + sz);
  memset(f, 0, sizeof(f));
  for (int i = (1); i <= (sz - 1); i++) {
    for (int j = n; j >= block[i]; j--) {
      if (f[j] < f[j - block[i]] + block[i]) f[j] = f[j - block[i]] + block[i];
    }
  }
  for (int i = (0); i <= (n); i++) {
    if (f[i] == i) {
      printf("%d\n", pos + i);
    }
  }
  return 0;
}
