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;
const int N = 1111;
int h[N], f[N], opt[N], a[N], b[N], c[N], d[N];
int n, m, i, j, k, x, ch;
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) f[i] = i;
  for (i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i]) f[find(i)] = f[find(b[i])];
  }
  for (i = 1; i <= n; i++)
    if (f[find(m)] != i) {
      int cnt = 0;
      for (j = 1; j <= n; j++)
        if (f[find(j)] == i) cnt++;
      if (cnt) a[++ch] = cnt;
    }
  opt[1] = 1;
  for (i = 1; i <= ch; i++)
    for (j = n; j >= a[i]; j--) opt[j] |= opt[j - a[i]];
  int cnt = 0, top = 0;
  for (i = 1; i <= n; i++)
    if (f[find(i)] == f[find(m)]) c[++cnt] = i;
  for (i = 1; i <= cnt; i++) {
    if (!b[c[i]] && !h[c[i]]) {
      d[++top] = c[i];
      for (j = 1; j <= cnt; j++)
        if (b[c[j]] == i) b[c[j]] = 0;
    }
  }
  for (x = 1; x <= top; x++)
    if (d[x] == m) break;
  x--;
  for (i = 1; i <= n - cnt + 1; i++)
    if (opt[i]) printf("%d\n", i + x);
}
